Wednesday 26 February 2014

Get user input at run-time to select Test ENV | Scanner | Selenium


In Java, Scanner is used to get the user input at run-time and act accordingly. Here, we make use of this to select the specific environment that we have to run. Please find the test class below..

SCANNER | Selenium


import java.util.Scanner;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class classname {
private WebDriver driver;
private String baseUrl;
private int a, i;

@BeforeTest
public void setUp() throws Exception {

int a;
Scanner in = new Scanner(System.in);
driver = new FirefoxDriver();

System.out.println("Please choose the Environment: \n");
System.out.println("1. DEV Environment");
System.out.println("2. QA Environment");
System.out.println("3. PROD Environment");

a = in.nextInt();

if (a == 1) {
System.out.println("You have selected 'DEV Environment' ...\n");
driver.get("http://dev-xyz.com/");
}
if (a == 2) {
System.out.println("You have selected 'QA Environment' ...\n");
driver.get("http://qa-xyz.com/");
}
if (a == 3) {
System.out
.println("You have selected 'PROD Cloud Environment' ...\n");
driver.get("http://prod-xyz.com/");
}

in.close();

driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
}

@Test
public void Test01() throws Exception {
// your code here...
}

@AfterTest
public void tearDown() throws Exception {
driver.quit();
}
}




3 comments:

  1. Hi Prashanth sams ,
    Your blog is really cool and refreshing. I would suggest you create driver object right after the the input processing. This will ensure the focus on both console and browser.

    //input processing
    a = in.nextInt();
    in.close();
    driver = new FirefoxDriver();

    ReplyDelete
  2. Thank you..
    It helped me a lot..!!

    ReplyDelete
  3. this code will not be run

    ReplyDelete