简体   繁体   中英

Not able to Run URL in different Browser using Selenium web driver in Switch case condition

//Package files that i have Used

    import org.testng.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.openqa.selenium.chrome.ChromeDriver;

//this is the java code in which i want to execute URL in chrome through the user input. at output time i enter option 2. to run URL in Chrome but it shows FAILED open browser

 @Test (priority = 1)
 public void openBrowser() {

System.out.println("-----Select Module-----");
System.out.println("1. Firefox");
System.out.println("2. Chrome");
System.out.println("3. Internet Explorer ");

int role;
try {
    role = Integer.parseInt(reader.readLine());



switch (role) {
case 1:

     driver = new FirefoxDriver();
    break;
case 2:
     driver = new ChromeDriver();
    break;
case 3:
     driver = new InternetExplorerDriver();
    break;
default:
    //System.out.println("browser : " + browserType + " is invalid, Launching Firefox as browser of choice..");
    driver = new FirefoxDriver();
}

} catch (NumberFormatException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  }

   @Test (priority = 2,groups = {"TC01"},description = "User would be able to       Login Successfully")
   @Parameters ({ "UserName","Password"})
public void SuccessfulLogin(String userName, String passWord) {

try {



    driver.get("http://180.211.114.147:97/Account/Login");
    driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
    driver.findElement(By.id("UserName")).sendKeys(userName);
    driver.findElement(By.id("Password")).sendKeys(passWord);

    driver.findElement(By.id("btnLogin")).submit();

        String tmp = driver.getCurrentUrl();
        if (tmp.equals("http://180.211.114.147:97/#/app/dashboard"))
        {
            System.out.println("Login success!!");
        }
        else{
            System.out.println("Fail to login..");}
        driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);

        /*String Actualtext = driver.findElement(By.xpath("/html/body/div/div[2]/div[3]/div[2]")).getText();
        Assert.assertEquals(Actualtext, "The username or password provided is incorrect");
        */

        //driver.quit();



} catch (Exception ex) {

  }
  }

here is the code of XML file

<suite name="Test Login" verbose="2">


<test name="Browser selection" >
<classes>
<class name="Login_Test">
<methods>
    <include name="openBrowser"></include>

</methods>
</class>
</classes>
</test>

<test name="Login with valid data" >

<parameter name="UserName" value="harsh"></parameter>
<parameter name="Password" value="harsh123"></parameter>

<groups>
<run>
<include name="TC01"></include>
</run>
</groups>

<classes>
<class name="Login_Test">
<methods>
    <exclude name="InvalidLogin"></exclude>
    <exclude name="EmptyLogin"></exclude>
</methods>
</class>
</classes>

</test>

Output of TestNG

[TestNG] Running:
  E:\sumit_rana\Files\Automation\Java eclipse\FAM_Test\Login.xml

-----Select Module-----
1. Firefox
2. Chrome
3. Internet Explorer 
2
FAILED: openBrowser
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
    at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
    at org.openqa.selenium.chrome.ChromeDriverService.access$0(ChromeDriverService.java:1)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
    at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116)
    at Login_Test.openBrowser(Login_Test.java:66)

Yes this is a simple initialization error. You need driver for executing your code on Chrome and IE.

And you need to set the system property and give the paths of your drivers correspondingly:

System.setProperty("webdriver.ie.driver", "Drivers//IEDriverServer.exe");
driver =  new InternetExplorerDriver();

System.setProperty("webdriver.chrome.driver", "Drivers//chromedriver.exe");
driver =  new ChromeDriver();

You can set system property both outside the switch or inside, your wish.

You can download the drivers from here: http://www.seleniumhq.org/download/

A good practice that I follow is making a folder named 'Drivers' in my project file and copying these files. This makes your project more portable.

You need to download the binary files for chrome and IE and then you also need to pass the path of the binary in the code.

For chrome download binary from below link according your configuration:-

http://chromedriver.storage.googleapis.com/index.html?path=2.21/

For Internet Explorer download from below link:-

http://www.seleniumhq.org/download/#mainContent (The Internet Explorer Driver Server)

Now use setProperty to set the path of the binary and then pass it to chromedriver object as below:-

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();

Hope it will help you :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM