Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 对两个testNG测试方法并行运行和同时运行进行计时?_Selenium_Parallel Processing_Testng - Fatal编程技术网

Selenium 对两个testNG测试方法并行运行和同时运行进行计时?

Selenium 对两个testNG测试方法并行运行和同时运行进行计时?,selenium,parallel-processing,testng,Selenium,Parallel Processing,Testng,我试图在我的站点上测试可能的竞争条件,因此希望两种testNG测试方法(使用selenium 2.0)在两个不同的浏览器中同时执行。我正在使用Eclipse的testNG插件(Indigo)。时间安排很重要,因为我想知道当同时执行两个更新时会发生什么,以及它是否会引发异常。我曾尝试将testng.xml中的“parallel”属性设置为类、测试和方法,但一个测试总是在另一个测试之前启动,因此它们不同步。另外,一个测试可能需要更长的时间来执行(几毫秒),因此我不确定如何获得正确的时间 目前,我有两

我试图在我的站点上测试可能的竞争条件,因此希望两种testNG测试方法(使用selenium 2.0)在两个不同的浏览器中同时执行。我正在使用Eclipse的testNG插件(Indigo)。时间安排很重要,因为我想知道当同时执行两个更新时会发生什么,以及它是否会引发异常。我曾尝试将testng.xml中的“parallel”属性设置为类、测试和方法,但一个测试总是在另一个测试之前启动,因此它们不同步。另外,一个测试可能需要更长的时间来执行(几毫秒),因此我不确定如何获得正确的时间

目前,我有两个不同类别的测试,因为这给了我最接近我想要的结果

第1类:

public class SyncTest {

WebDriver driver;
WebElement element;


@BeforeMethod
public void setUp() throws Exception {

    driver = new FirefoxDriver();

}


@Test(timeOut = 15000)
public void testSync() throws Exception {
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    //Login
    driver.get("http://mySite.com");
    driver.findElement(By.name("username")).sendKeys("admin");
    driver.findElement(By.name("password")).sendKeys("admin");
    try {
    element = driver.findElement(By.id("centralLogin_0"));
    element.click();
    } catch (Exception e){
        System.out.println("Error submitting: " + e.toString());
    }

    //open issue
    driver.findElement(By.linkText("Sync Test")).click();
    driver.switchTo().frame("mobile_vault");
    //Change status
    Select select = new Select(driver.findElement(By.id("statusSelect")));
    //select.deselectAll();
    select.selectByVisibleText("Open");
    List <WebElement> elements;
    //driver.findElement(By.className("button nextDialogue")).click();
    try {
    driver.findElement(By.cssSelector("div.button.nextDialogue > div")).click();
    } catch (Exception e){
        System.out.println("Not found");
    }

    Thread.sleep(2000);    
   try {
    driver.findElement(By.xpath("//form[@id='frmcall']/fieldset/div[14]/div[2]/div[1]")).click();
   } catch (Exception e) {
       System.out.println("Not Found");
       System.out.println(e.toString());
   }

   Thread.sleep(3000);
   driver.navigate().refresh();





}





@AfterMethod
public void tearDown() {
    driver.quit();
}
我意识到我使用testNG注释可能是不正确的,因为我不确定哪种注释是最好的(@BeforeSuite、@BeforeMethod等)

我的testng.xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="classes">
<test name="Test" preserve-order="false">
<classes>
  <class name="syncTest.SyncTest"/>
  <class name = "syncTest.NewSyncTest"/>


</classes>
</test>
</suite>

正如我所说,测试从来不会完全并行运行,我不知道如何实现这一点:(我还将在下面包括testng-results.xml,请注意每个测试方法的时间:

<test name="Test" duration-ms="20264" started-at="2012-02-27T15:52:02Z" finished-    at="2012-02-27T15:52:23Z">
  <class name="syncTest.SyncTest">
    <test-method status="PASS" signature="setUp()[pri:0, instance:syncTest.SyncTest@3e9d75c5]" name="setUp" is-config="true" duration-ms="8475" started-at="2012-02-27T15:52:02Z" finished-at="2012-02-27T15:52:11Z">
    </test-method>
    <test-method status="PASS" signature="testSync()[pri:0, instance:syncTest.SyncTest@3e9d75c5]" name="testSync" duration-ms="11556" started-at="2012-02-27T15:52:11Z" finished-at="2012-02-27T15:52:22Z">
    </test-method>
    <test-method status="PASS" signature="tearDown()[pri:0, instance:syncTest.SyncTest@3e9d75c5]" name="tearDown" is-config="true" duration-ms="217" started-at="2012-02-27T15:52:22Z" finished-at="2012-02-27T15:52:23Z">
    </test-method>
  </class>
  <class name="syncTest.NewSyncTest">
    <test-method status="PASS" signature="beforeMethod()[pri:0, instance:syncTest.NewSyncTest@539ef8a0]" name="beforeMethod" is-config="true" duration-ms="4345" started-at="2012-02-27T15:52:02Z" finished-at="2012-02-27T15:52:07Z">
    </test-method>
    <test-method status="PASS" signature="testF()[pri:0, instance:syncTest.NewSyncTest@539ef8a0]" name="testF" duration-ms="9728" started-at="2012-02-27T15:52:07Z" finished-at="2012-02-27T15:52:16Z">
    </test-method>
    <test-method status="PASS" signature="afterMethod()[pri:0, instance:syncTest.NewSyncTest@539ef8a0]" name="afterMethod" is-config="true" duration-ms="231" started-at="2012-02-27T15:52:16Z" finished-at="2012-02-27T15:52:17Z">
    </test-method>
  </class>
</test>


欢迎提出任何建议。提前感谢!:)

试图通过“完全同时”进行两次访问来测试比赛条件可能是徒劳的,也没有用。无用,因为你永远无法实现它,无用,因为当访问发生在“完全相同的时间”,但当原子操作(通常是读和写)以两个进程获得的共享变量的视图不一致的方式交错时,数据竞争不会发生

如果您有两个进程,每个进程执行一次读取,然后从/写入同一个变量,请考虑4个原子操作可能发生的所有方式(R1-进程1读取,等等):

等等


重写测试,以便可以控制每个进程上R和W之间的间隔,使这些间隔足够长,以控制原子操作的交错,并检查您的程序是否能够处理。

竞争条件测试不能像High Performance Mark所指出的那样具有确定性。另一方面,如果您想测试作为多个线程并行执行的单个测试方法,那么您可以尝试如下所示的方法

public class ConcurrentTestRunnerTest {

    @Test(threadPoolSize = 3, invocationCount = 10,  timeOut = 10000)
    public void shouldRunInParallel1() {
        System.out.println("I'm running on thread " + Thread.currentThread().getName());
    }

    @Test(threadPoolSize = 3, invocationCount = 10,  timeOut = 10000)
    public void shouldRunInParallel2() {
        System.out.println("I'm running on thread " + Thread.currentThread().getName());
    }

}
这将确保您配置的线程数量与并行执行的线程数量相同


在我看来,您应该能够使用一些性能测试工具(如JMeter)获得这样的结果。刚才提到这一点是因为您正在使用硒。

感谢您提供以下答案!高性能标记,我明白你的意思。我将解释我要测试的内容:我在数据库中有一组数据,并且有两个数据视图(中央和客户机)。这两个视图显然需要同步。其中一个(由用户)所做的更改将反映在另一个中。我上面的第一个测试用例像用户一样编辑表单,然后单击submit。第二个调用web操作,该操作检查所有项目的编辑并将其保存到DB。如果用户进行了编辑,并且同时进行了此检查,该编辑是否会丢失?检查时不会看到编辑?您尝试解释的这种情况似乎是一种情况。它可以很容易地克服使用锁。有关更多信息,请参阅。另外,在处理典型小型测试(即使并发)的单元测试环境中重现这种情况是非常困难的。
R1,W1,R2,W2
R1,R2,W2,W1
public class ConcurrentTestRunnerTest {

    @Test(threadPoolSize = 3, invocationCount = 10,  timeOut = 10000)
    public void shouldRunInParallel1() {
        System.out.println("I'm running on thread " + Thread.currentThread().getName());
    }

    @Test(threadPoolSize = 3, invocationCount = 10,  timeOut = 10000)
    public void shouldRunInParallel2() {
        System.out.println("I'm running on thread " + Thread.currentThread().getName());
    }

}