为什么在Selenium IDE中运行测试时单击了一个链接,但是当nunit.exe运行测试时没有单击它?

时间:2013-05-18 18:05:18

标签: c# nunit selenium-webdriver

我有以下Selenium测试:

open    /dealerproducts.preprod/MainPage.aspx   
type    id=ucTopBar_ASPxRoundPanel2_ucLogin_cbLogin_pnl_txtLoginEmail_I tester@gmail.com
type    id=ucTopBar_ASPxRoundPanel2_ucLogin_cbLogin_pnl_txtPwd_I    pass123
clickAndWait    css=#ucTopBar_ASPxRoundPanel2_ucLogin_cbLogin_pnl_btnLogin_CD > span    
click   link=Search Contract    
selectFrame     id=ContentPlaceHolder1_tabPages_frContent1  
pause       1000
click   id=grdList_header2_colFilter_2_txtValue1_2  
type    id=grdList_header2_colFilter_2_txtValue1_2  PM10000130
click   css=span    
click   id=grdList_cell0_2_lnkNumber_0

当我在Selenium IDE中运行此测试时,始终会单击最后一个链接(id = grdList_cell0_2_lnkNumber_0)。 但是当nunit.exe运行以下测试(上面的测试导出到c#)时,不会点击最后一个链接:

[TestFixture]
public class ElanceTestWebDriver
{
    private IWebDriver driver;
    private StringBuilder verificationErrors;
    private string baseURL;
    private bool acceptNextAlert = true;

    [SetUp]
    public void SetupTest()
    {
        driver = new FirefoxDriver();
        baseURL = "http://mysite.com/";
        verificationErrors = new StringBuilder();
    }

    [Test]
    public void TheElanceTestWebDriverTest()
    {
        driver.Navigate().GoToUrl(baseURL + "/dealerproducts.preprod/MainPage.aspx");
        driver.FindElement(By.Id("ucTopBar_ASPxRoundPanel2_ucLogin_cbLogin_pnl_txtLoginEmail_I")).Clear();
        driver.FindElement(By.Id("ucTopBar_ASPxRoundPanel2_ucLogin_cbLogin_pnl_txtLoginEmail_I")).SendKeys("tester@gmail.com");
        driver.FindElement(By.Id("ucTopBar_ASPxRoundPanel2_ucLogin_cbLogin_pnl_txtPwd_I")).Clear();
        driver.FindElement(By.Id("ucTopBar_ASPxRoundPanel2_ucLogin_cbLogin_pnl_txtPwd_I")).SendKeys("pass123");
        driver.FindElement(By.Id("ucTopBar_ASPxRoundPanel2_ucLogin_cbLogin_pnl_btnLogin_CD")).Click();
        driver.FindElement(By.LinkText("Search Contract"), 2).Click();
        driver.SwitchTo().Frame("ContentPlaceHolder1_tabPages_frContent1");
        driver.FindElement(By.Id("grdList_header2_colFilter_2_txtValue1_2")).Click();
        driver.FindElement(By.Id("grdList_header2_colFilter_2_txtValue1_2")).Clear();
        driver.FindElement(By.Id("grdList_header2_colFilter_2_txtValue1_2")).SendKeys("PM10000130");
        driver.FindElement(By.CssSelector("span")).Click();
        IWebElement el = null;
        el = driver.FindElement(By.Id("grdList_cell0_2_lnkNumber_0")); //.Click();
        Assert.That(el != null); // 1.
        driver.FindElement(By.Id("grdList_cell0_2_lnkNumber_0")).Click();
    }

运行上述测试时始终会找到元素id = grdList_cell0_2_lnkNumber_0,因为断言1.永远不会失败。奇怪的是,当在Visual Studio中以调试模式逐步运行此测试时,始终会单击该链接。 链接的HTML:

<a class="dxeHyperlink" onclick="aspxSEClick('grdList_cell0_2_lnkNumber_0', event)" id="grdList_cell0_2_lnkNumber_0" style="font-size:9px;">PM10000130</a>

有人可以告诉我为什么nunit.exe或nunit-x86.exe运行测试时没有点击此链接?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,我不得不添加

Thread.Sleep(new TimeSpan(0, 0, 3));

点击链接之前。