【问题标题】:Webdriver object gets overwritten when tests are run in parallel并行运行测试时,Webdriver 对象被覆盖
【发布时间】:2016-01-27 21:06:45
【问题描述】:

我正在编写基于 Java 的 selenium-web-driver 测试,以使用 testng 运行并行跨浏览器测试。 我已将测试设置为在我的 xml 文件上并行运行。文件如下所示:

 <suite name="TestSuite" thread-count="2" parallel="tests" >    
 <test name="ChromeTest">
  <parameter name="browser" value="Chrome" />               
   <classes>
      <class name="test.login"/>
      <class name="test.main"/>
      <class name="test.logout"/> 
   </classes>  
 </test>

<test name="FirefoxTest">
 <parameter name="browser" value="Firefox" />              
   <classes>
      <class name="test.login"/>
      <class name="test.main"/>
      <class name="test.logout"/> 
   </classes>  
 </test>

但是当我运行测试时,两个浏览器实例都会打开(Chrome 首先打开并开始执行,然后延迟 Firefox 打开)。 在这种情况下,驱动程序对象被 Firefox 驱动程序覆盖,chrome 停止执行。测试在 Firefox 和 成功完成。

项目的结构是这样的:

  • 创建了一个 driverbase.class 来加载与我的 @Beforesuite 浏览器对应的驱动程序。
  • 为页面创建了单独的类。(例如:login.class、main.class 等)只有 @Test 方法并扩展了 driverbase 类来获取驱动程序。

当我在 xml 文件上将并行设置为无时,测试成功运行

<suite name="TestSuite" thread-count="2" parallel="none" >

我该如何克服这个问题?如何在没有这个问题的情况下并行运行测试?

driverbase类是这样的:

public class driverbase {
	
	  private String baseUrl;
	  private String nodeUrl;
	  private boolean acceptNextAlert = true;
	  private StringBuffer verificationErrors = new StringBuffer();
	  
	public static WebDriver driver = null;
		     		  
	  	/**
		 
	   * This function will execute before each Test tag in testng.xml

	   * @param browser

	   * @throws Exception

	   */
	      	  
	@BeforeSuite

	@Parameters("browser")

	  public WebDriver setup(String browser) throws Exception{
		
	      //Check if parameter passed from TestNG is 'firefox'
			
	      if(browser.equalsIgnoreCase("firefox")){
	    	  
	    	  System.out.println("Browser  : "+browser);
	    	      	
	    	  FirefoxProfile profile = new FirefoxProfile();
	 		  profile.setAcceptUntrustedCertificates(true);
	 		
	 		  //create firefox instance
	 		  driver = new FirefoxDriver(profile);
	 			  
	      }
	      
	      //Check if parameter passed as 'chrome'

	      else if(browser.equalsIgnoreCase("chrome")){

	    	  System.out.println("Browser  : "+browser);
	    	      	  
	    //set path to chromedriver.exe You may need to download it from http://code.google.com/p/selenium/wiki/ChromeDriver

	          System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");

	          ChromeOptions options = new ChromeOptions();
	          options.addArguments("--test-type");
	        
	 			//create chrome instance
		          
	        	  driver = new ChromeDriver(options);
	        	
	      }
	    
	       else{

	          //If no browser passed throw exception
	    	   
	    	   System.out.println("Browser  is incorrect");

	          throw new Exception("Browser is not correct");

	       }
	  
	      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
	      driver.manage().window().maximize();
		  return driver;
	  
	}

感谢您的帮助:)

【问题讨论】:

标签: java selenium-webdriver testng selenium-chromedriver selenium-firefoxdriver


【解决方案1】:
  1. @BeforeSuite 方法不应该返回一些东西。 => 替换为void
  2. 您的 testng 有 2 个不同的测试,但 @BeforeSuite 将始终由套件运行一次,您的评论表明您并不期望它。 => 替换为@BeforeTest
  3. 当您在 // 中运行时,有 2 个线程正在设置驱动程序值(一个使用 firefox,一个使用 chrome)来解释您的问题。

你可以试试这样的:

public class driverbase {

  private String baseUrl;
  private String nodeUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();   
  public WebDriver driver;

  @BeforeTest
  @Parameters("browser")
  public void setup(String browser) throws Exception {
      if(browser.equalsIgnoreCase("firefox")) {
          FirefoxProfile profile = new FirefoxProfile();
          profile.setAcceptUntrustedCertificates(true);
          driver = new FirefoxDriver(profile);
      } else if(browser.equalsIgnoreCase("chrome")) {
          System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
          ChromeOptions options = new ChromeOptions();
          options.addArguments("--test-type");
          driver = new ChromeDriver(options);
      } else {
          throw new Exception("Browser is not correct");
      }

      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
      driver.manage().window().maximize();
}

你也应该看看http://fluentlenium.org/

【讨论】:

  • 我试过了。但没有成功。我可以用 parallel = false 运行测试。在这种情况下,两个测试都运行成功。
  • 在我的示例中,属性不是线程安全的。您可以将它们包装到 ThreadLocal 或使用 @Factory 而不是之前的类方法。
  • 找到了一个使用 HashMap 的解决方案。现在我可以并行运行测试了。
  • 你能发布一个解决方案吗?
【解决方案2】:

确保不会将同一个 webdriver 实例分配给多个测试。创建驱动程序实例的方法应该是同步的。这应该可以解决问题。

public synchronized void setup(String browser) throws Exception {
      if(browser.equalsIgnoreCase("firefox")) {
          FirefoxProfile profile = new FirefoxProfile();
          profile.setAcceptUntrustedCertificates(true);
          driver = new FirefoxDriver(profile);
      } else if(browser.equalsIgnoreCase("chrome")) {
          System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
          ChromeOptions options = new ChromeOptions();
          options.addArguments("--test-type");
          driver = new ChromeDriver(options);
      } else {
          throw new Exception("Browser is not correct");
      }

      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
      driver.manage().window().maximize();
}

【讨论】:

    猜你喜欢
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 2016-11-09
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    相关资源
    最近更新 更多