【问题标题】:TesstNg parallel execution concatenate the input test data in one browserTestNg 并行执行在一个浏览器中连接输入测试数据
【发布时间】:2018-06-26 23:09:48
【问题描述】:

我在一个类中有两个测试用例。当使用 TestNg 并行运行时,它会连接所有测试数据并将其放在一个浏览器中,而另一个浏览器为空。

硒版本:3.8.21 火狐:最新 测试:6.8.21

以下是代码

package cli;

import java.net.MalformedURLException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class test1 {
    public WebDriver driver;

    @BeforeMethod
    public void setUp() throws MalformedURLException {
        System.out.println("@BeforeMethod: The annotated method will be run before each test method.");

    }

    @Test
    public void test() throws MalformedURLException, InterruptedException {
        driver = jenkinHub.hub();
        driver.get("https://www.google.com/");
        driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys("Testing");
        System.out.println("This is current Url " + driver.getCurrentUrl());
        Thread.sleep(9000);
        driver.quit();

    }

    @Test
    public void NotTesting() throws MalformedURLException, InterruptedException {
        driver = jenkinHub.hub();
        driver.get("https://www.google.com/");
        driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys("Not Testing");
        System.out.println("This is current Url " + driver.getCurrentUrl());
        Thread.sleep(9000);
        driver.quit();

    }

}

xml文件

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Regression Suite" verbose="1" parallel="methods" thread-count="3"> 

<test name="REgressionSuite"  group-by-instances="true">

    <classes>
    <class name="cli.test1" /> 
    </classes>
  </test>
</suite>

【问题讨论】:

    标签: selenium testng


    【解决方案1】:

    可能是因为您的driver 是类级别字段。当第一个测试方法初始化它 (driver = jenkinHub.hub()) 并尝试使用它时,另一个方法会重新为其分配新值,因此您的所有操作都使用相同的 WebDriver 实例完成。
    尝试在这样的测试方法中移动它

    @Test
    public void test() throws MalformedURLException, InterruptedException {
    
        WebDriver driver = jenkinHub.hub(); // create WebDriver instance
    
        driver.get("https://www.google.com/");
        driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys("Testing");
        System.out.println("This is current Url " + driver.getCurrentUrl());
        Thread.sleep(9000);
        driver.quit();
    
    }
    
    @Test
    public void NotTesting() throws MalformedURLException, InterruptedException {
    
        WebDriver driver = jenkinHub.hub(); // create another WebDriver instance
    
        driver.get("https://www.google.com/");
        driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys("Not Testing");
        System.out.println("This is current Url " + driver.getCurrentUrl());
        Thread.sleep(9000);
        driver.quit();
    
    }
    

    【讨论】:

      【解决方案2】:

      这里是如何并行执行的示例,您可以根据需要更改驱动程序实例化方法。试试这个,然后对你的代码进行相应的调整,它会打开两个 chrome 并在其中搜索测试项,并且不会连接。

      package Grid
      import java.net.MalformedURLException;
      import java.net.URL;
      
      import org.openqa.selenium.By;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.remote.DesiredCapabilities;
      import org.openqa.selenium.remote.RemoteWebDriver;
      import org.testng.annotations.BeforeMethod;
      import org.testng.annotations.Test;
      
      public class test1 {
      
          DesiredCapabilities caps;
      
      
      
          @BeforeMethod
          public void setUp() throws MalformedURLException {
              System.out.println("@BeforeMethod: The annotated method will be run before each test method.");
              caps = new DesiredCapabilities();
              caps = DesiredCapabilities.chrome();
      
          }
      
          @Test
          public void testing() throws MalformedURLException, InterruptedException {
              WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps);
              driver.get("https://www.google.com/");
              driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys("Testing");
              System.out.println("This is current Url " + driver.getCurrentUrl());
              Thread.sleep(9000);
              driver.quit();
      
          }
      
          @Test
          public void NotTesting() throws MalformedURLException, InterruptedException {
              WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps);
              driver.get("https://www.google.com/");
              driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys("Not Testing");
              System.out.println("This is current Url " + driver.getCurrentUrl());
              Thread.sleep(9000);
              driver.quit();
      
          }
      
      
          }
      

      testng.xml 文件(与编辑包名相同)

      <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
      <suite name="Regression Suite" verbose="1" parallel="methods" thread-count="4"> 
      
      <test name="REgressionSuite">
      
          <classes>
          <class name="Grid.test1" /> 
          </classes>
        </test>
      </suite>
      

      【讨论】:

      • HI PJAutomator,为@BeforeTest 中的每个测试用例创建了新的驱动程序实例。但仍然有同样的问题。
      • @MSohaibKhan 你也可以粘贴 jenkinHub 吗?
      • @PJAutomator。刚刚也添加了 jenkinHub。
      • @MSohaibKhan 我贴了一个例子。请使用它,然后相应地调整您的代码,
      • @MSohaibKhan ,我遇到了同样的问题。你找到解决办法了吗?
      猜你喜欢
      • 2016-05-19
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      相关资源
      最近更新 更多