我正在使用Selenium Web驱动程序-Java编写跨浏览器测试脚本。我的firefox,chrome和IE浏览器正在打开并成功运行脚本。但是,在歌剧中,只有浏览器正在打开。即使driver.manage().window().maximize();也无法正常工作。只需打开浏览器并停留直到我关闭它。当我手动关闭浏览器时,测试套件失败。

这是我的java类。

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("[email protected]");
        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 [email protected]")){
            System.out.println("User Successfully logged in");

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

        driver.quit();

    }

}


这是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 -->


我已经从this页面(operadriver_win64.zip)下载了Operadriver。

这是Opera浏览器的打开方式。

java - 歌剧驱动程序不适用于Selenium-Java-LMLPHP

Opera版本为41.0.2353.56。

提前致谢。 :)

最佳答案

找出答案。

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


然后安装了歌剧38。问题解决了。 :)

07-25 23:20