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

问题描述 投票:0回答:1

我有大约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>

但是,当我尝试在方法级别上运行时,我遇到了问题:

<?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>

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

此设置也不起作用:

<?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>

这是测试:

@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());
}

知道为什么类可以并行化,但方法不能?

java selenium-webdriver webdriver testng parallel-testing
1个回答
0
投票

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();
  }
}

此外,在gradle文件中,请确保定义您正在使用的testng XML文件的名称:

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

TestNG文件:

<!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>
© www.soinside.com 2019 - 2024. All rights reserved.