当并发线程在不同的浏览器窗口中填充表单时,只填充一个表单(并非完全填写)(when concurrent threads fill a form in different browser window

编程入门 行业动态 更新时间:2024-10-07 07:26:00
当并发线程在不同的浏览器窗口中填充表单时,只填充一个表单(并非完全填写)(when concurrent threads fill a form in different browser windows, only one form is filled (and not completely either))

首先,我最近才熟悉Selenium / WebDriver / Maven / TestNG。 这是情况:

我需要测试一个webapp。 第一页有一个要填写的表格; 后续页面有各种其他操作。 我有一个类(比如App.java )使用Selenium WebDriver自动化大部分webapp。 从Selenium文档中学习,我让Maven创建了我的项目并将其导入eclipse 。 我现在需要在N个并行线程中执行一些测试用例; 这将调用早期类中的各种方法。 (基于测试用例) 为此,我使用Selenium Grid / Maven / TestNG使用从此链接收集的提示。 (当然还有Grid和TestNG文档。) 设置完所有后,我有以下架构: 一个App.java (和帮助程序类),用于浏览webapp的每个页面; 一个AppTest.java (和Test01...N.java ),它有一个调用App.java中各种方法的测试用例。

这是AppTest.java:

public class AppTest { protected ThreadLocal<RemoteWebDriver> threadDriver = null; @BeforeMethod public void setUp() throws Exception { threadDriver = new ThreadLocal<RemoteWebDriver>(); DesiredCapabilities dc = DesiredCapabilities.chrome(); threadDriver.set(new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dc)); } public WebDriver getDriver() { return threadDriver.get(); } @AfterMethod public void closeBrowser() { getDriver().quit(); } }

这是Test01.java

public class TestGroup01 extends AppTest { //Doc wasnt clear, but with the values below, I got this testcase to be called 3 times in parallel @Test(threadPoolSize = 3, invocationCount = 3, timeOut = 10000) public void TestCase1() throws Exception { App.doSomething(getDriver()); Thread.sleep(2000); }

这是App.doSomething

public static WebDriver driver; public class App { public static void doSomething(WebDriver drv) throws Exception { driver = drv; //I did this to avoid having to pass drv to every method drv.get(URL_TO_TEST); AppHelper.enterData(); // this method now accesses driver } }

问题

测试用例打开3个浏览器窗口(如预期的那样)。 加载URL并打开表单(如预期的那样)。 但是只有其中一个窗口输入了任何数据。 在该窗口中,只填充一个字段。 我完全无法弄清楚为什么会这样。 App.doSomething方法设置静态变量driver以避免必须将drv传递给每个方法。 这是某种原因吗?

First off, I only recently got acquainted Selenium/WebDriver/Maven/TestNG. Here's the situation:

I need to test a webapp. The first page has a form to be filled; subsequent pages have various other actions. I have a class (say App.java )that automates most of the webapp using Selenium WebDriver. Learning from the Selenium docs, I had Maven create my project and import it into eclipse. I now need to execute some test-cases in N parallel threads; which will call various methods from the earlier class. (based on the testcase) For this, I used Selenium Grid/Maven/TestNG using tips gleaned from this link. (and of course, Grid and TestNG documentation.) After setting everything up, I had the following architecture: an App.java (and helper classes) that does the legwork of navigating each page of the webapp; an AppTest.java (and Test01...N.java) that has testcases which call various methods in App.java.

Here's AppTest.java:

public class AppTest { protected ThreadLocal<RemoteWebDriver> threadDriver = null; @BeforeMethod public void setUp() throws Exception { threadDriver = new ThreadLocal<RemoteWebDriver>(); DesiredCapabilities dc = DesiredCapabilities.chrome(); threadDriver.set(new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dc)); } public WebDriver getDriver() { return threadDriver.get(); } @AfterMethod public void closeBrowser() { getDriver().quit(); } }

Here's Test01.java

public class TestGroup01 extends AppTest { //Doc wasnt clear, but with the values below, I got this testcase to be called 3 times in parallel @Test(threadPoolSize = 3, invocationCount = 3, timeOut = 10000) public void TestCase1() throws Exception { App.doSomething(getDriver()); Thread.sleep(2000); }

And here's App.doSomething

public static WebDriver driver; public class App { public static void doSomething(WebDriver drv) throws Exception { driver = drv; //I did this to avoid having to pass drv to every method drv.get(URL_TO_TEST); AppHelper.enterData(); // this method now accesses driver } }

Problem

The testcase opens up 3 browser windows (as expected). The URL gets loaded and the form opens (as expected). But only one of those windows has any data entered into it. And in that window, only one field is filled. I am completely unable to figure out why this is happening. The App.doSomething method is setting a static variable driver to avoid having to pass drv to every method. Is this somehow a cause?

最满意答案

此行为是由于传递给@Test注释的timeout参数。

@Test(threadPoolSize = 3, invocationCount = 3, timeOut = 10000)

该参数确定测试可以运行的最长时间。 由于我已将其设置为10000毫秒,因此测试在此之后被静默中止。

This behavior was due to the timeout parameter passed to the @Test annotation.

@Test(threadPoolSize = 3, invocationCount = 3, timeOut = 10000)

The parameter determines the maximum time that a test can run. Since I had set it to 10000ms, the test was being silently aborted after that time.

更多推荐

App,java,public,WebDriver,电脑培训,计算机培训,IT培训"/> <meta name="des

本文发布于:2023-08-07 16:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465263.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   线程   浏览器   窗口中   concurrent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!