2

我正在编写基于Java的selenium-web-driver测试,以使用testng运行并行跨浏览器测试。 我已在我的XML文件。该文件运行并行测试看起来是这样的:当并行运行测试时Webdriver对象被覆盖

<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方法和扩展驱动程序类来获取驱动程序。

测试的suceessfully运行,当我在XML文件

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

平行设置为none我怎样才能解决这个问题?如何在没有这个问题的情况下并行运行测试?

的driverbase类是这样的:

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

 
\t * @param browser 
 

 
\t * @throws Exception 
 

 
\t */ 
 
\t  \t 
 
\t @BeforeSuite 
 

 
\t @Parameters("browser") 
 

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

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

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

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

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

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

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

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

感谢您的帮助:)

+0

你可以发布driverbase.java吗? – Mona

+0

添加了驱动程序代码片段 – Sijin

+0

看看:https://github.com/Ardesco/Selenium-Maven-Template – Ardesco

回答

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/看看了。

+0

我试过了,但没有成功。我能够运行与parallel = false的测试。在这种情况下测试运行成功。 – Sijin

+0

在我的示例中,属性不是线程安全的。您可以将它们包装到ThreadLocal中,或者使用@Factory而不是Before类方法。 – juherr

+0

找到了一个使用HashMap的解决方案。现在我能够并行运行测试。 – Sijin

相关问题