javaselenium-webdriveroperaoperadriver

opera driver is not working with Selenium - Java


I'm writing cross browser test script using Selenium web driver - Java. My firefox, chrome and IE browsers are opening and successfully running the script. But, in opera, only the browser is opening. Even driver.manage().window().maximize(); also not working. Just open the browser and stay until I close it. When I close the browser manually, test suite fails.

Here is my java class.

package multiBrowser;

import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.testng.annotations.Parameters;

public class MultiBrowserClass {

    WebDriver driver;

    @Test
    @Parameters("browser")
    public void multiBrowsers(String browserName) throws InterruptedException{

        if(browserName.equalsIgnoreCase("firefox")){

            System.setProperty("webdriver.firefox.marionette","D:\\My Work\\Setup\\JAR\\geckodriver.exe");
            ProfilesIni profile = new ProfilesIni();    
            FirefoxProfile myprofile = profile.getProfile("default");
            driver = new FirefoxDriver(myprofile);

        }
        if(browserName.equalsIgnoreCase("chrome")){

            System.setProperty("webdriver.chrome.driver", "D:\\My Work\\Setup\\JAR\\driver\\chromedriver.exe");
            driver = new ChromeDriver();
        }
        else if(browserName.equalsIgnoreCase("IE")){

            System.setProperty("webdriver.ie.driver", "D:\\My Work\\Setup\\JAR\\driver\\IEDriverServer.exe");
            driver = new InternetExplorerDriver();
        }
        else if(browserName.equalsIgnoreCase("opera")){

            System.setProperty("webdriver.opera.driver", "D:\\My Work\\Setup\\JAR\\driver\\operadriver.exe");
            driver = new OperaDriver(); 

        }

        driver.manage().window().maximize();

        driver.navigate().to("https://");

        System.out.println(driver.getTitle());

        driver.findElement(By.xpath("//div[@id='navbar-main']/ul/li[5]/a")).click();    
        driver.findElement(By.xpath("//div[@id='navbar-main']/ul/li[5]/ul/li/a")).click();
        Thread.sleep(3000);
        driver.findElement(By.name("email")).clear();
        driver.findElement(By.name("email")).sendKeys("abc@mm.kk");
        driver.findElement(By.name("password")).clear();
        driver.findElement(By.name("password")).sendKeys("1qaz2wsx");
        Thread.sleep(3000);
        driver.findElement(By.xpath("//form[@id='loginform']/div[8]/button")).click();
        Thread.sleep(5000);

        if(driver.getPageSource().contains("Welcome abc@mm.kk")){
            System.out.println("User Successfully logged in");

        }else{
            System.out.println("Username or password you entered is incorrect");
        }

        driver.quit();

    }

}

Here is the testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">

  <!-- firefox -->  

  <test name="FirefoxTestCases">  
      <parameter name="browser" value="firefox"/>  
      <classes> 
        <class name="multiBrowser.MultiBrowserClass"/> 
      </classes> 
  </test> 


  <!-- chrome -->  

  <test name="ChromeTestCases">  
      <parameter name="browser" value="chrome"/>  
      <classes> 
        <class name="multiBrowser.MultiBrowserClass"/> 
      </classes> 
  </test>


   <!-- internet explorer -->

  <test name="IETestCases">  
      <parameter name="browser" value="IE"/>  
      <classes> 
         <class name="multiBrowser.MultiBrowserClass"/> 
      </classes> 
  </test>

     <!-- Opera -->

  <test name="OperaTestCases">  
      <parameter name="browser" value="opera"/>  
      <classes> 
         <class name="multiBrowser.MultiBrowserClass"/> 
      </classes> 
  </test>

</suite> <!-- Suite -->

I've downloaded operadriver from this page (operadriver_win64.zip).

Here is how Opera browser is opening.

Opera Browser

Opera version is 41.0.2353.56.

Thanks in advance. :)


Solution

  • Figured out the answer.

    System.setProperty("webdriver.chrome.driver", "D:\\My Work\\Setup\\JAR\\driver\\operadriver.exe");
    driver = new ChromeDriver();
    

    Then installed opera 38. Problem solved. :)