更改同一URL上的Xpath会使登录测试中的Assert失败 [英] Changing Xpath on the same URL makes Assert in Login Test to fail

查看:125
本文介绍了更改同一URL上的Xpath会使登录测试中的Assert失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为网页登录网址创建了一个负面测试用例。

I have created a negative test case for a webpage login URL.

在LoginPage(java)中我定义了一个Webelement(alertMessage),在点击登录之前Firepath是:

In LoginPage(java) I have defined a Webelement(alertMessage), before hit login Firepath is :

<div id="submit-box">
<div id="loginAlert" class="loginAlert"/>
</form>

在命中Firepath-ul后修改因为用户错误:

After Hit Firepath-ul is modifing because the user is wrong:

<div id="submit-box">
<div id="loginAlert" class="loginAlert">User doesn't exist.</div>
</form>

登录后网址保持不变。

The URL remains the same after hit login.

在Maven中运行测试,我有Assert错误:

Running the test in Maven, I have Assert error:

java.lang.AssertionError: 
Expected: a string containing "Wrong password."
     but: was ""
Expected :a string containing "Wrong password."

Actual   :""
 <Click to see difference>

我的LoginPage java包:

My java package for LoginPage:

package CsphEbox.Pages;

import Resources.PageBase;
import com.gargoylesoftware.htmlunit.javascript.host.Console;
import com.sun.org.apache.xerces.internal.impl.xs.identity.Field;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import static javax.swing.text.html.CSS.getAttribute;
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOf;


public class LoginPage extends PageBase

{
    @FindBy (id = "UserName")
    private WebElement loginUserNameInput;
    @FindBy (id = "UserPassword")
    private WebElement loginUserPasswordInput;
    @FindBy (id = "Submit")
    private WebElement loginButton;
    @FindBy(xpath = ".//*[@id='loginAlert']")
    private WebElement alertMessage;


    // private WebElement alertMessage;
    //String source = driver.findElement(By.xpath(".//form[@id='login']//div[@class='loginAlert' and @id='loginAlert']")).getAttribute("innerHTML");

    public LoginPage(WebDriver driver)

    { super(driver);
      waitForURLToContains("/login/login");
    }

    public String getAlertMessage()

    {
         waitForElementToBeVisible(alertMessage);
         return alertMessage.getAttribute("innerHTML");

    }

    public LoginPage loginWrongAs(String username, String password)

    {
        waitForElementToBeVisible(loginUserNameInput);
        loginUserNameInput.sendKeys(username);
        loginUserPasswordInput.sendKeys(password);
        loginButton.click();
        return new LoginPage(driver);
    }

我的LoginTest java包:

My java package for LoginTest:

package CsphEbox.Test;

import CsphEbox.Pages.ActionEboxPage;
import CsphEbox.Pages.LoginPage;
import Resources.TestBase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static CsphEbox.Utils.LoginUtils.*;
import static java.lang.Thread.sleep;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class LoginTest extends TestBase

{  private LoginPage loginPage;

   @Before

    public void setUp()

    {
        driver.get(LOGIN_PAGE_URL);
        loginPage = new LoginPage(driver);

    }

    @Test

    public void loginWithWrongPassword() throws Exception

    {

        LoginPage eboxLog = loginPage.loginWrongAs(USERNAME,WRONGPASSWORD);
        assertThat(eboxLog.getAlertMessage(),containsString("Wrong password."));
        sleep(2000);
    }

我正在使用Java和Selenium Webdriver。

I am using Java with Selenium Webdriver.

有没有人知道WebElement的 alertMessage 的xpath没有使用LoginAlert消息更新:密码错误。 ??

Does anyone knows with the xpath for the WebElement alertMessage is not updating with the LoginAlert message: "Wrong password." ??

谢谢!

推荐答案

在LoginClass中需要它它是一个计时器。

In LoginClass it was need it a timer.

public String loginWrongAs(String username,String password)

public String loginWrongAs(String username, String password)

{
    waitForElementToBeVisible(loginUserNameInput);1
    loginUserNameInput.sendKeys(username);
    loginUserPasswordInput.sendKeys(password);
    loginButton.click();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException ie) {
        //Handle exception
    }


     WebElement webu = driver.findElement(By.className("loginAlert"));
     WebElement webumic = webu.findElement(By.id("loginAlert"));
     String source = webu.getText();
     return source;



}

这篇关于更改同一URL上的Xpath会使登录测试中的Assert失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆