Elements<> doesn't work properly when several threads work with same list
Opened this issue · 0 comments
See example:
public class BaseTest {
static int flag = 0;
@BeforeMethod(alwaysRun = true)
public void setUp() throws IOException {
initFromProperties();
init(useDriver("chrome"), YandexSite.class);
}
@Test
public void testSample1() throws InterruptedException {
YandexSite.yandexPage.open();
YandexSite.yandexPage.searchField.input("av.by");
System.out.println("First result for search [av.by]: " + YandexSite.yandexPage.autoSuggestItems.get(0).getText());
flag = 1;
while(flag == 1){Thread.sleep(1000);}
System.out.println("First result for search [av.by]: " + YandexSite.yandexPage.autoSuggestItems.get(0).getText());
}
@Test
public void testSample2() throws InterruptedException {
YandexSite.yandexPage.open();
YandexSite.yandexPage.searchField.input("cloud");
while (flag == 0){Thread.sleep(1000);/*wait first thread init autosuggest items*/}
System.out.println("First result for search [cloud]: " + YandexSite.yandexPage.autoSuggestItems.get(0).getText());
flag = 0;
}
@AfterMethod(alwaysRun = true)
public void tearDown(){
WebSettings.getDriver().quit();
}
}
@JSite("https://www.yandex.by/")
public class YandexSite extends WebSite {
public static YandexPage yandexPage;
}
@JPage("")
public class YandexPage extends WebPage {
@XPath("//input[contains(@class,'input__control')]")
public TextField searchField;
@XPath("//li[contains(@class,'suggest2-item')]")
public Elements<Button> autoSuggestItems;
}
pom.xml
<dependencies>
<!--JDI-->
<dependency>
<groupId>com.epam.jdi</groupId>
<artifactId>jdi-uitest-web</artifactId>
<version>1.1.18</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/suite.xml</suiteXmlFile>
</suiteXmlFiles>
<reportsDirectory>${project.basedir}/test-output</reportsDirectory>
</configuration>
</plugin>
</plugins>
</build>
<suite name="test suite" parallel="tests" thread-count="10">
<test name="Test1" preserve-order="true">
<classes>
<class name="example.BaseTest"/>
<methods>
<include name="testSample1" />
</methods>
</classes>
</test>
<test name="Test2" preserve-order="true">
<classes>
<class name="example.BaseTest"/>
<methods>
<include name="testSample2" />
</methods>
</classes>
</test>
</suite>
run mvn clean install
Result output:
37:29.313 INFO: [ThreadID: 12]Input text 'av.by' in text field for TextField 'Search Field' (YandexPage.searchField; ; xpath='//input[contains(@Class,'input__control')]')
37:29.920 INFO: [ThreadID: 12]Get text for Button 'Button' (.Button; Has WebElement)
37:29.968 INFO: [ThreadID: 12]Get result 'av.by' in 0.05 seconds
First result for search [av.by]: av.by
37:33.606 INFO: [ThreadID: 13]Input text 'cloud' in text field for TextField 'Search Field' (YandexPage.searchField; ; xpath='//input[contains(@Class,'input__control')]')
37:33.714 INFO: [ThreadID: 13]Is element displayed for Button 'Button' (.Button; Has WebElement)
37:33.741 INFO: [ThreadID: 13]Get result 'true' in 0.03 seconds
37:33.750 INFO: [ThreadID: 13]Get text for Button 'Button' (.Button; Has WebElement)
37:33.772 INFO: [ThreadID: 13]Get result 'av.by' in 0.02 seconds
First result for search [cloud]: av.by
37:33.971 INFO: [ThreadID: 12]Is element displayed for Button 'Button' (.Button; Has WebElement)
37:33.997 INFO: [ThreadID: 12]Get result 'true' in 0.03 seconds
37:34.01 INFO: [ThreadID: 12]Get text for Button 'Button' (.Button; Has WebElement)
37:34.21 INFO: [ThreadID: 12]Get result 'av.by' in 0.02 seconds
First result for search [av.by]: av.by