使用SeleniumWebDriver自动创建登录页面

使用SeleniumWebDriver自动创建登录页面,selenium,selenium-webdriver,automation,Selenium,Selenium Webdriver,Automation,我正在使用SeleniumWeb驱动程序自动化angular应用程序登录页面,我需要一些验证的解决方案 我试图识别元素并找到了,但是在验证登录字段时,它忽略了if条件 这是我的密码 un = driver.findElement(By.xpath("//*[@id='mat-input-0']")); un.sendKeys(""); Thread.sleep(500); boolean une = un.getText().isEmpty(); pw = driver.findElem

我正在使用SeleniumWeb驱动程序自动化angular应用程序登录页面,我需要一些验证的解决方案

我试图识别元素并找到了,但是在验证登录字段时,它忽略了if条件

这是我的密码

un = driver.findElement(By.xpath("//*[@id='mat-input-0']"));

un.sendKeys("");

Thread.sleep(500);

boolean une = un.getText().isEmpty();

pw = driver.findElement(By.xpath("//*[@id='mat-input-1']"));

pw.sendKeys("Password123");

Thread.sleep(1000);

boolean pwe = pw.getText().isEmpty();

//System.out.println("text--->"+un.getText());

//System.out.println(pw);

//WebElement element=pw;

//String u = un.getText();

//String p = un.getText();

//if(un!=null && pw!=null && (!un.getText().isEmpty()&&!pw.getText().isEmpty()))
//if(un!=null && pw!=null && (une!=true && pwe!=true))
//if((un!=null && un.getText()!=null &&  !un.getText().isEmpty()) && (pw!=null && pw.getText()!=null && !pw.getText().isEmpty()))

if(un!=null && pw!=null && (une=false) && (pwe=false))

{ 
    driver.findElement(By.xpath("//button[@class='eta-float mat-raised-button mat-primary']")).click();

    Thread.sleep(1000);

    //logout menu
    driver.findElement(By.xpath("/html/body/app-root/div/mat-toolbar/mat-toolbar-row/button[3]")).click();

    Thread.sleep(2000);

    //logout option
    driver.findElement(By.xpath("/html/body/div/div[2]/div/div/div/button[1]"));

    driver.quit();
}
else
{
    System.out.println("Please provide valid credentials!");
}

    everytime only else part is executed

您的程序中存在逻辑错误。而不是=您必须使用==

因此,这可能会导致条件始终失败,因此else部分始终在执行

对于WebElement,如果两个元素都显示并且都包含文本,我们可以使用如下所示

if(un.isDisplayed()&&pw.isDisplayed()&&(!une) && (!pwe))

您的代码存在一些问题:

un和pw永远不会为空。如果.findElement找不到该元素,它将抛出ElementNotFoundException,而不是返回null,因此您可以跳过这些条件

不验证两个字段是否为空。真的没有意义。即使这两个字段都不是空的,并且用户名或密码不正确,您仍然需要处理登录失败的问题。跳过尝试验证空字段,而是尝试登录。如果登录失败,您可以通过查找一些登录失败消息来判断,然后测试失败

如果要按ID查找元素,请使用by.ID而不是by.xpath

与XPath相比,更喜欢CSS选择器,因为它们更快、受支持更好、更易于阅读

使用Thread.sleep是一种不好的做法。用谷歌搜索一下原因。而是使用WebDriverWait

不要使用超过3级或以HTML开头的XPath。它们可能会因HTML中的任何微小更改而中断

您应该使用TestNG或JUnit进行验证和日志记录。不要写你自己的或只是写消息到控制台。它不可维护

您的代码应该类似于

String username = "";
String password = "Password123";

driver.findElement(By.id("mat-input-0")).sendKeys(username);
driver.findElement(By.id("mat-input-1")).sendKeys(password);
driver.findElement(By.cssSelector("button.eta-float.mat-raised-button.mat-primary")).click(); // I'm assuming this is the login button?
// check for a successful login message
if (driver.findElements(loginSuccessMessageLocator).isEmpty())
{
    // login success message NOT found
    // fail the test
}
else
{
    // login success message found
    driver.findElement(By.xpath("/html/body/app-root/div/mat-toolbar/mat-toolbar-row/button[3]")).click();
    driver.findElement(By.xpath("/html/body/div/div[2]/div/div/div/button[1]")); // this doesn't do anything... is it supposed to click?
}

我检查un和pwd字段不为null,它不为空,但每次,else部分单独执行pw.sendKeys;如何检查此条件如何验证字段以及您能否指定要检查的条件check@Parth-检查新的解决方案-但您现有的代码是错误的。amit,如何验证sendkeyspw.sendkeys传递的空字符串-这意味着什么?这意味着什么pw.sendkeyspw.sendkeyspassword2意味着pw是一个输入html元素,您正在尝试在pw中键入文本Password2。如何通过sendkeys传递空值或如何验证没有通过sendkeys传递的数据
String username = "";
String password = "Password123";

driver.findElement(By.id("mat-input-0")).sendKeys(username);
driver.findElement(By.id("mat-input-1")).sendKeys(password);
driver.findElement(By.cssSelector("button.eta-float.mat-raised-button.mat-primary")).click(); // I'm assuming this is the login button?
// check for a successful login message
if (driver.findElements(loginSuccessMessageLocator).isEmpty())
{
    // login success message NOT found
    // fail the test
}
else
{
    // login success message found
    driver.findElement(By.xpath("/html/body/app-root/div/mat-toolbar/mat-toolbar-row/button[3]")).click();
    driver.findElement(By.xpath("/html/body/div/div[2]/div/div/div/button[1]")); // this doesn't do anything... is it supposed to click?
}