我正在尝试使用testng和硒并行执行测试。当我触发测试时,它在一个会话上成功执行,而在另一个会话上却挂起了..在日志中看到以下错误。
我尝试在testng.xml中将方法/类/测试用作并行选项。在所有情况下都是相同的问题。
Exception in thread "pool-1-thread-1" org.openqa.selenium.WebDriverException: java.net.SocketException: Software caused connection abort: recv failed
Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:24:21.231Z'
System info: host: 'PF12FNSG20HE', ip: '146.27.24.164', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_171'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:92)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:605)
at org.openqa.selenium.remote.RemoteWebDriver.getScreenshotAs(RemoteWebDriver.java:294)
at AEMSites.Base.getScreenShot(Base.java:186)
at resources.TestListeners.onTestFailure(TestListeners.java:36)
at org.testng.internal.Invoker.runTestListeners(Invoker.java:1895)
at org.testng.internal.Invoker.runTestListeners(Invoker.java:1879)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1292)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
我的代码如下:
POM文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>AEMSites</groupId>
<artifactId>ChevronECOM</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>ChevronECOM</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.13.0</version>
</dependency>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>compile</scope>
</dependency>
<!-- Extent Reports -->
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>3.1.5</version>
</dependency>
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>2.21.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<useSystemClassLoader>true</useSystemClassLoader>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<properties>
<property>
<name>parallel</name>
<value>methods</value>
</property>
<property>
<name>threadCount</name>
<value>2</value>
</property>
<property>
<name>dataproviderthreadcount</name>
<value>2</value>
</property>
</properties>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.15</version>
</plugin>
</plugins>
</build>
</project>
测试Ng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name=" test suite" parallel="methods" thread-count="2">
<listeners>
<listener class-name="resources.TestListeners"/>
<listener class-name="ExtentReportsChevron"/>
<listener class-name="resources.RetryListenerClass"/>
</listeners>
<test name="Chrome Tests" parallel="methods" thread-count="2">
<classes>
<class name="AEMSites.TechronCleanHomePageTests"/>
<class name="AEMSites.TechronCleanContactUsPageTests"/>
<class name="AEMSites.TechronCleanFAQTests"/>
<class name="AEMSites.TechronCleanWhereToBuyPageTests"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
我的测试:
package AEMSites;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import AEMSites.pageObjects.TechronCleanHomePage;
import AEMSites.pageObjects.TechronCleanSearchResultsPage;
public class TechronCleanHomePageTests extends Base
{
TechronCleanHomePage tch;
TechronCleanSearchResultsPage tcsrp;
//public static String experience;
@BeforeMethod
public void HomePageNavigation(Method method) throws Throwable
{
//FileInputStream fis = new FileInputStream("C:\\Users\\hpak\\ChevronECOM\\Variables.properties");
//prop.load(fis);
//experience=prop.getProperty("experience");
System.out.println("Test Executing is: " + method.getName());
driver=initializeDriver();
System.out.println("step1" + driver);
System.out.println("step1" + url);
driver.get(url);
System.out.println("step2");
System.out.print("Expericene in test method is"+experience);
if(experience.equalsIgnoreCase("mobile"))
{
System.out.println("In Home page tests before method in mobile");
tch = new TechronCleanHomePage(driver);
tch.getMobileHamburgerMenu().click();
}
else
{
driver.manage().window().maximize();
}
}
@Test(groups = {"HomePage"})
public void TC_014_VerifyProductsLinkOnHomePage() throws InterruptedException
{
tch = new TechronCleanHomePage(driver);
System.out.println("In test case method, experience is "+experience);
if(experience.equalsIgnoreCase("mobile"))
{
tch.getProducts_mobile().click();
TimeUnit.SECONDS.sleep(10);
}
else
{
System.out.println("In desktop method");
tch.getProducts().click();
}
Assert.assertTrue((tch.getCurrentWindowUrl()).contains("products.html"));
}
@Test(groups = {"HomePage"})
public void TC_043_VerifyWhyTechronLinkOnHomePage()
{
tch = new TechronCleanHomePage(driver);
tch.getWhyTechron().click();
Assert.assertTrue((tch.getCurrentWindowUrl()).contains("why-techron.html"));
}
@Test(groups = {"HomePage"})
public void TC_002_VerifySearchAndVisitProductPage()
{
System.out.println("In search test");
tch = new TechronCleanHomePage(driver);
tcsrp = new TechronCleanSearchResultsPage(driver);
tch.getSearchIcon().click();
tch.getSearchBar().sendKeys("Motor Oil");
tch.getSearchBar().sendKeys(Keys.RETURN);
try
{
Assert.assertTrue(driver.getPageSource().contains("Motor Oil"));
Assert.assertTrue(driver.getPageSource().contains("Search Results"));
Assert.assertTrue(driver.getPageSource().contains("Techron Complete Fuel System Cleaner"));
}
catch (Error e)
{
System.out.println(e);
}
tcsrp.getProductLink().click();
try
{
Assert.assertTrue((tch.getCurrentWindowUrl()).contains("techron-complete-fuel-system"));
Assert.assertTrue(driver.getPageSource().contains("Concentrate Plus Complete Fuel System Cleaner"));
}
catch (Error e)
{
System.out.println(e);
}
}
@Test(groups = {"HomePage"})
public void TC_012_VerifyProductLineupOnHomePage()
{
tch = new TechronCleanHomePage(driver);
String findproductext=tch.getFindProductText().getText();
System.out.print("one"+findproductext);
Assert.assertTrue(findproductext.contains("FIND A PRODUCT"));
String imagesrc=tch.getImageSource().getAttribute("src");
System.out.print("two"+imagesrc);
Assert.assertTrue(imagesrc.contains("/content/dam"));
tch.getImageSource().click();
String imagesrcafterclick=tch.getImageSource().getAttribute("src");
System.out.print("three"+imagesrcafterclick);
Assert.assertTrue(imagesrcafterclick.contains("/content/dam"));
Assert.assertTrue(tch.getLearnMoreBtnOnPrdLineUp().isDisplayed());
}
@Test(groups = {"HomePage"})
public void TC_041_VerifyWhereToBuyOnHomePage()
{
tch = new TechronCleanHomePage(driver);
tch.getWhereToBuy().click();
Assert.assertTrue((tch.getCurrentWindowUrl()).contains("where-to-buy.html"));
}
@Test(groups = {"HomePage"})
public void TC_046_VerifyFAQLinkOnHomePage()
{
tch = new TechronCleanHomePage(driver);
tch.getFAQ().click();
Assert.assertTrue((tch.getCurrentWindowUrl()).contains("faq.html"));
}
@Test(groups = {"Footer"})
public void TC_003_VerifyContactUsLinkOnFooter()
{
tch = new TechronCleanHomePage(driver);
tch.getContactUs().click();
System.out.println("Current Window url is"+tch.getCurrentWindowUrl());
Assert.assertTrue((tch.getCurrentWindowUrl()).contains("contact-us.html"));
}
@Test(groups = {"HomePage"})
public void TC_010_VerifyMarketFormOnHomePage() throws InterruptedException
{
tch = new TechronCleanHomePage(driver);
tch.getMarketoFirstName().sendKeys("TestFirst");
tch.getMarketoLastName().sendKeys("TestLast");
tch.getMarketoEmail().sendKeys("ignore@gmail.com");
tch.getMarketoSubmitBtn().click();
TimeUnit.SECONDS.sleep(5);
System.out.println("Current Window url is"+tch.getCurrentWindowUrl());
Assert.assertTrue((tch.getCurrentWindowUrl()).contains("aliId"));
}
@Test(groups = {"Footer"})
public void TC_004_VerifyTermsOfUseLinkOnFooter()
{
tch = new TechronCleanHomePage(driver);
tch.getTermsOfUse().click();
Assert.assertTrue((tch.getCurrentWindowUrl()).contains("terms-of-use"));
Assert.assertTrue(driver.getPageSource().contains("terms of use"));
}
@Test(groups = {"Footer"})
public void TC_005_VerifyPrivacyPolicyOnFooter()
{
tch = new TechronCleanHomePage(driver);
tch.getPrivacyPolicy().click();
Assert.assertTrue((tch.getCurrentWindowUrl()).contains("privacy"));
Assert.assertTrue(driver.getPageSource().contains("privacy statement"));
}
@Test (groups = {"Footer"})
public void TC_006_VerifyChevronLubricantsLinkOnFooter()
{
tch = new TechronCleanHomePage(driver);
tch.getChevronLubricants().click();
Assert.assertTrue((tch.getCurrentWindowUrl()).contains("https://www.chevronlubricants.com/"));
}
@Test (enabled=false)
public void VerifyTabItemsOnHomePage()
{
List<WebElement> allLinks = ((WebDriver) driver).findElements(By.tagName("a"));
System.out.println("In Verify Tab items test");
System.out.println(allLinks);
for (WebElement w : allLinks)
{
w.click();
if (((WebDriver) driver).findElement(By.xpath("Element on the page")).isDisplayed())
{
System.out.println("Link:"+w.getText()+"is working");
}
else
{
System.out.println("Link:"+w.getText()+"is not working");
}
((WebDriver) driver).navigate().back();//To come back to the Home screen
}
}
@AfterMethod
public void closeAll()
{
//System.out.println("Test:" + result.getMethod().getMethodName()+ " is "+status.toString().toUpperCase());
driver.quit();
//driver.close();
driver=null;
}
}
有人可以建议吗?
答案 0 :(得分:0)
尝试与
一起运行@BeforeMethod(alwaysRun = true)
public void HomePageNavigation(Method method)
和
@AfterMethod(alwaysRun = true)
public void closeAll()