2012-02-27 132 views
0

我想在我的网站上测试可能的竞争条件,因此希望两个testNG测试方法(使用selenium 2.0)在两个不同的浏览器中完全同时执行。我正在使用Eclipse的testNG插件(Indigo)。时机很重要,因为我想知道当两次更新同时执行时会发生什么,以及是否会抛出异常。我试着将testng.xml中的'parallel'属性设置为类,测试和方法,但是一个测试总是先于另一个测试,所以它们不同步。另外一个测试可能需要更长的时间来执行(几毫秒),所以我不确定如何让时机恰到好处。定时两个testNG测试方法并行运行并在同一时间运行?

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

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

}

这2类:

public class NewSyncTest { 

WebDriver driver; 
WebElement element; 

    @Test 
    public void testF() 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 admin page 
    driver.get("http://mySite.com/admin"); 

    Thread.sleep(2000); 



try { 

    driver.findElement(By.cssSelector("input[type='submit']")).click(); 

} catch (Exception e){ 
     System.out.println("Not found"); 
    } 



    } 
    @BeforeMethod 
    public void beforeMethod() { 

    driver = new FirefoxDriver(); 
    } 

    @AfterMethod 
    public void afterMethod() { 

    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:[email protected]]" 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:[email protected]]" 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:[email protected]]" 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:[email protected]]" 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:[email protected]]" 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:[email protected]]" 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> 

任何和所有的建议,欢迎。提前致谢! :)

+0

感谢您的答案!高性能标志,我明白你的观点。我将解释我想要测试的内容:我在数据库中有一组数据,并且有两个视图(中央和客户端)。这两个观点显然需要同步。其中一个(由用户)所做的更改反映在另一个中。我上面的第一个测试用例编辑了一个表单,用户可以点击提交。第二个调用web动作,检查所有项目的编辑并将其保存到数据库。如果用户进行编辑并且同时进行此项检查,那么编辑是否会丢失?编辑将不会被支票看到? – user1088166 2012-02-28 10:56:01

+0

您试图解释的这种情况看起来像[幻像读取](http://en.wikipedia.org/wiki/Isolation_(database_systems)#Phantom_reads)情况。它可以很容易地用锁来克服。有关更多信息,请参阅[this](http://docstore.mik.ua/orelly/java-ent/ebeans/ch08_03.htm)。 – Bala 2012-03-01 08:18:59

+0

另外,在单元测试环境中重现这种情况非常困难,该环境通常处理小型测试(不管何时并发)。 – Bala 2012-03-01 08:20:41

回答

1

试图通过在'完全同一时间'发生两次访问来测试竞赛条件可能是徒劳的练习,也没有用。无用的,因为你永远不会实现它,没用,因为数据竞赛不会在'完全同时'出现访问时出现,但当原子操作(通常是读和写)交错时,共享变量的视图这两个过程得到的是不一致的。

如果你有2个进程,每个进行读写操作,然后写入/来自同一个变量,请考虑代替4个原子操作可能发生的所有方式(R1 - 进程1读取等) :

R1,W1,R2,W2 
R1,R2,W2,W1 

重写你的测试,这样你可以控制每一个进程R和W之间的间隔,使这些间隔足够长的时间来控制原子操作的交织,并检查你的程序对应。

1

正如高性能标记所指出的那样,对于竞态条件的测试不能确定。另一方面,如果你想测试一个并行执行的多个线程的测试方法,那么你可以尝试如下所示。

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

} 

这将确保您拥有并行执行的配置线程数量。

IMO,你应该能够使用一些性能测试工具,如JMeter,实现这样的结果。刚才提到这是因为你使用硒。