Selenium Firefox驱动程序通过测试但实际上没有点击按钮

时间:2016-08-25 15:35:57

标签: java html selenium firefox cucumber

我正在使用Selenium和Cucumber在Firefox和Chrome中进行测试。这些测试在Chrome中运行良好。但是,在Firefox中我遇到了一个有趣的问题。下面的场景中的每一步都说它通过了:

Feature: Login Action

Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User enters "admin" and "password"
Then Message displayed Login Successfully

Scenario: Successful LogOut
When User LogOut from the Application
Then Message displayed LogOut Successfully
And I close the browser

然而,当它到达“来自应用程序的用户logut”时,它实际上从未注销。它说按钮被点击但我仍然记录。

@When("^User LogOut from the Application$")
public void user_LogOut_from_the_Application() throws Throwable {

    driver.findElement(By.id("titlebar_hyperlink_8-lbsignout")).click();
    System.out.println("The click worked");
}


@And("^I close the browser$")
public void i_close_the_browser() throws Throwable {

    System.out.println("Driver= " + driver);
    driver.close();
    System.out.println("The browser should be closed");
}

它执行最终版本,但是浏览器没有关闭,我仍然登录到网站。

该元素的结构如下所示(原谅我从页面源复制和粘贴的格式)

<table role="presentation" align="right" style="height: 100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td>
            <span id="titlebar-tb_username" class="homeButtontxtappname userfullname" style="display:none">MAXADMIN</span>
        </td>    
        <td id="titlebar_bulletinboardlink_3-co_0"          role='presentation'      nowrap="nowrap"      style="vertical-align:top;">

            <span id="titlebar_bulletinboardlink_3-lbshowbboard" align='left' ctype="label"    tabindex="0"      targetid="titlebar_bulletinboardlink_3-lbshowbboard"  mxevent="click" accesskey='B' class="text powerwhite   anchor" style="display:block;cursor:pointer;" title="Bulletins: &#040;0&#041;  ALT+B">
                <img id='titlebar_bulletinboardlink_3-lbshowbboard_image' src='btn_bboard.gif' class='pwimg' border='0'  style='vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;' alt=''/>
                <span>
                    <span/>
                    <span class='text hl hlak'>B</span>
                    <span>ulletins: (0) </span>
                </span>
            </span>

        </td>
        <td id="titlebar_hyperlink_5-co_0" role='presentation'      nowrap="nowrap"   align="left"   style="vertical-align:top;">

            <span id="titlebar_hyperlink_5-lbshowmenu_reportsmenu" align='left' ctype="label"    tabindex="0"    ev="reportsmenu"  targetid="titlebar_hyperlink_5-lbshowmenu_reportsmenu"  mxevent="click" accesskey='R' class="text powerwhite   anchor" style="display:block;cursor:pointer;" title="Reports ALT+R">
                <img id='titlebar_hyperlink_5-lbshowmenu_reportsmenu_image' src='btn_reporting.gif' class='pwimg' border='0'  style='vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;' alt=''/>
                <span>
                    <span/>
                    <span class='text hl hlak'>R</span>
                    <span>eports</span>
                </span>
            </span>
        </td>
        <td id="titlebar_hyperlink_7-co_0" role='presentation'             nowrap="nowrap"   align="left"   style="vertical-align:top;">
            <span id="titlebar_hyperlink_7-lbshowmenu_profile" align='left' ctype="label"    tabindex="0"    ev="profile"  targetid="titlebar_hyperlink_7-lbshowmenu_profile"  mxevent="click" accesskey='P' class="text powerwhite   anchor" style="display:block;cursor:pointer;" title="Profile ALT+P">
                <img id='titlebar_hyperlink_7-lbshowmenu_profile_image' src='btn_profile.gif' class='pwimg' border='0'  style='vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;' alt=''/>
                <span>
                    <span/>
                    <span class='text hl hlak'>P</span>
                    <span>rofile</span>
                </span>
            </span>
        </td>
        <td id="titlebar_hyperlink_8-co_0" role='presentation'      nowrap="nowrap"   align="left"   style="vertical-align:top;">
            <span id="titlebar_hyperlink_8-lbsignout" align='left' ctype="label"    tabindex="0"      targetid="titlebar_hyperlink_8-lbsignout"  mxevent="click" accesskey='S' class="text powerwhite   anchor" style="display:block;cursor:pointer;" title="Sign Out ALT+S">
                <img id='titlebar_hyperlink_8-lbsignout_image' src='btn_signout.gif' class='pwimg' border='0'  style='vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;' alt=''/>
                <span>
                    <span/>
                    <span class='text hl hlak'>S</span>
                    <span>ign Out</span>
                </span>
            </span>
        </td>
        <td id="titlebar_hyperlink_9-co_0" role='presentation'      nowrap="nowrap"   align="left"   style="vertical-align:top;">

            <span id="titlebar_hyperlink_9-lbshowmenu_help" align='left' ctype="label"    tabindex="0"    ev="help"  targetid="titlebar_hyperlink_9-lbshowmenu_help"  mxevent="click" accesskey='H' class="text powerwhite   anchor" style="display:block;cursor:pointer;" title="Help ALT+H">
                <img id='titlebar_hyperlink_9-lbshowmenu_help_image' src='btn_help.gif' class='pwimg' border='0'  style='vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;' alt=''/>
                <span>
                    <span/>
                    <span class='text hl hlak'>H</span>
                    <span>elp</span>
                </span>
            </span>
        </td>

    </tr>
</table>

我没有收到任何错误,它说我通过了,但我仍然没有退出。

编辑:我试图利用以下解决方案,但没有运气

WebElement element = driver.findElement(By.id("titlebar_hyperlink_8-lbsignout_image"));
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("arguments[0].click();", element);
    wait.until(
            ExpectedConditions.visibilityOfElementLocated(By.
                    xpath("//img[@id='titlebar_hyperlink_8-lbsignout_image']")));

这是Chrome控制台的日志。

 document.getElementById("titlebar_hyperlink_8-lbsignout_image")
    <img id=​"titlebar_hyperlink_8-lbsignout_image" src=​"btn_signout.gif" class=​"pwimg" border=​"0" style=​"vertical-align:​top;​margin:​0px;​margin-left:​3px;​margin-right:​3px;​" alt>​
    document.getElementById("titlebar_hyperlink_8-lbsignout_image").click
click() { [native code] }
    document.getElementById("titlebar_hyperlink_8-lbsignout").click();
undefined
    document.getElementById("titlebar_hyperlink_8-lbsignout_image")
<img id=​"titlebar_hyperlink_8-lbsignout_image" src=​"btn_signout.gif" class=​"pwimg" border=​"0" style=​"vertical-align:​top;​margin:​0px;​margin-left:​3px;​margin-right:​3px;​" alt>​
document.getElementById("titlebar_hyperlink_8-lbsignout_image").click();
undefined

1 个答案:

答案 0 :(得分:0)

经过多次测试,我找到了答案。虽然titlebar_hyperlink_8-lbsignout在Chrome中作为选择器工作,但我显然不得不在firefox上更深入一级,现在它可以运行了。

//set the wait driver inside local scope just to be safe. 
wait = new WebDriverWait(driver, 60); 
     Thread.sleep(3000);
    wait.until(ExpectedConditions
            .elementToBeClickable(By.id("titlebar_hyperlink_8-lbsignout_image")));
    System.out.println(driver.findElement(By.id("titlebar_hyperlink_8-lbsignout_image")));
    driver.findElement(By.id("titlebar_hyperlink_8-lbsignout_image")).click();
    System.out.println("This is where I should have pressed the logout button");