简体   繁体   English

为什么 TestNG Selenium 并行方法不起作用?

[英]Why TestNG Selenium parallel by method doesn't work?

I have around 13 test classes, and this setting runs classes in parallel just fine:我有大约 13 个测试类,这个设置可以并行运行类:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Office Suite" thread-count="5" parallel="classes">
    <test name="Office Test" allow-return-values="true">
        <packages>
            <package name="testSuite.*"/>
        </packages>
    </test>
</suite>

However, when I am trying to run on a method level, I get problems:但是,当我尝试在方法级别上运行时,我遇到了问题:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Office Suite" thread-count="5" parallel="methods">
    <test name="Office Test" allow-return-values="true">
        <packages>
            <package name="testSuite.*"/>
        </packages>
    </test>
</suite>

I start seeing org.openqa.selenium.NoSuchElementException: Timed out after 5 seconds. Unable to locate the element我开始看到org.openqa.selenium.NoSuchElementException: Timed out after 5 seconds. Unable to locate the element org.openqa.selenium.NoSuchElementException: Timed out after 5 seconds. Unable to locate the element . org.openqa.selenium.NoSuchElementException: Timed out after 5 seconds. Unable to locate the element

Also this setting doesn't work:此设置也不起作用:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Office Suite" thread-count="5" parallel="methods">
    <test name="Office Test" allow-return-values="true">
        <classes>
            <class name="testSuite.CustomerProfileBillingTest"/>
            <class name="testSuite.CustomerProfileTest"/>
            <class name="testSuite.CustomerSearchPageTest"/>
            <class name="testSuite.HomePageTest"/>
            <class name="testSuite.HomeZoneTest"/>
            <class name="testSuite.ImportLogTest"/>
            <class name="testSuite.InfleetPageTest"/>
            <class name="testSuite.LoginPageTest"/>
            <class name="testSuite.MembersArea1Test"/>
            <class name="testSuite.MembersArea2Test"/>
            <class name="testSuite.MembersArea3Test"/>
            <class name="testSuite.PromoPageTest"/>
            <class name="testSuite.TicketsTest"/>
        </classes>
    </test>
</suite>

Here is test:这是测试:

@BeforeMethod(alwaysRun = true)
public void setUp() throws Exception {
    DriverFactory.french = false;
    WEB_DRIVER_THREAD_LOCAL.set(new EventFiringWebDriver(DriverFactory.getDriver(TestUtil.getTargetBrowser())).register(new WebEventListener()));

    on = new Pages(WEB_DRIVER_THREAD_LOCAL.get());
    wait = new WebDriverWait(WEB_DRIVER_THREAD_LOCAL.get(), TestUtil.ELEMENT_PRESENT_WAIT);
}

@Test(groups = {"registration"})
public void verifyCustomerCanRectifyError() throws Exception{
    on.MembersRegistrationPage()
            .typeIn(on.MembersRegistrationPage().firstNameInput, customer.getFirstName())
            .click(on.MembersRegistrationPage().continueButton)
            .waitForVisibilityOfElement(on.MembersRegistrationPage().requestDrRecordButtonXPATH);

    assertTrue(on.MembersRegistrationPage().requestDrRecordHeader.isDisplayed());
}

Any idea why classes can be parallelized, but methods can't?知道为什么类可以并行化,但方法不能吗?

NoSuchElementException means the driver is not finding an element and is timing out (which doesn't seem to be related to running tests in parallel. I suspect your issue is related to the way you are instantiating the browser, but I can't really tell, since you have references to code that appears to live elsewhere (eg: WEB_DRIVER_THREAD_LOCAL ). This works fine for me (tested using gradle): NoSuchElementException意味着驱动程序没有找到一个元素并且正在超时(这似乎与并行运行测试无关。我怀疑你的问题与你实例化浏览器的方式有关,但我真的不能告诉,因为您引用了似乎存在于其他地方的代码(例如: WEB_DRIVER_THREAD_LOCAL )。这对我来说很好(使用 gradle 测试):

public class Sample {
  private static ThreadLocal<RemoteWebDriver> drivers = new ThreadLocal<>();

  @BeforeMethod
   public void setUp() throws MalformedURLException {
     RemoteWebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"), DesiredCapabilities.chrome());
     drivers.set(driver);
  }

  @Test
  public void testOne() {
    System.out.println("Launching Thread [" + Thread.currentThread().getId() + "]");
    getDriver().get("https://www.google.com");
  }

  @Test
  public void testTwo() {
    System.out.println("Launching Thread [" + Thread.currentThread().getId() + "]");
    getDriver().get("https://www.stackoverflow.com");
  }

  @AfterMethod
  public void tearDown() {
    getDriver().quit();
  }

  @AfterClass
  public void removeThread() {
    drivers.remove();
  }

  private RemoteWebDriver getDriver() {
    return drivers.get();
  }
}

Also, in your gradle file, make sure you define the name of the testng XML file you are using:此外,在你的 gradle 文件中,确保你定义了你正在使用的 testng XML 文件的名称:

test {
  useTestNG() {
    useDefaultListeners = true 
    suites 'testng.xml' //location of the testng file
  }
}

TestNG file:测试NG文件:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >

<suite name="Sample" parallel="methods" thread-count="2" >
  <test name="SampleTest" >
    <classes>
        <class name="Sample" />
    </classes>
  </test>
</suite>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM