Java 需要验证用户登录后登录到正确的页面吗?
我正在本地系统上测试这一点,并试图验证用户是否成功登录并登录到正确的页面。 尝试将字符串数据与webelement数据进行比较时出错Java 需要验证用户登录后登录到正确的页面吗?,java,selenium,Java,Selenium,我正在本地系统上测试这一点,并试图验证用户是否成功登录并登录到正确的页面。 尝试将字符串数据与webelement数据进行比较时出错 `package wnsautomation; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; //import org.openqa.selenium.WebElement; import or
`package wnsautomation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class login {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\Users\\orange\\Downloads\\geckodriver.exe");
driver= new FirefoxDriver();
WebDriverWait myWait = new WebDriverWait(driver, 10);
String baseUrl = "http://192.168.1.52:9000";
driver.get(baseUrl);
myWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[2]/div/div/input")));
driver.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[2]/div/div/input")).sendKeys("admin@gmail.com");
driver.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[3]/div/div/input")).sendKeys("8JXzwRs4VWeGP0Sy");
driver.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[5]/button")).click();
String expectedtext="Summary";
WebElement actualtext;
actualtext = driver.findElement(By.linkText("/html/body/div[3]/div/ng-include/div/div/div[1]/div/h3"));
if (actualtext.contentEquals(expectedtext)){
System.out.println("User succesfully loggedIN");
} else {
System.out.println("Invalid credtendials!!");
}
}
}`
不要使用
Webelement
,而是将String
与getText()
方法结合使用,这样它将给出实际的字符串。不是Webelement
注意:不要使用absolute
xpath,而是使用relative
xpath。有关xpath
教程的更多详细信息,请参阅本教程
试试这个-
字符串expectedtext=“Summary”;
字符串actualtext=null
actualtext = driver.findElement(By.xpath("/html/body/div[3]/div/ng-include/div/div/div[1]/div/h3")).gettext();
但我不想用页面标题验证登录。标题“Summary”上有文字,我希望通过此文字进行用户验证,以及用户登录到正确的页面。请编辑您的问题,以提供您使用此代码遇到的具体错误。是的,我使用了相同的代码,但未验证此条件
if(expectedtext.equalsIgnoreCase(actualtext)){System.out.println(“用户成功登录”);}else{System.out.println(“无效的credtendials!!”);}
摘要
您的意思是什么,否则条件不起作用??,如果如果条件为执行
,则将不执行否则
条件。执行此代码时,您是否遇到任何错误?如果您的查询已解决,请将此答案标记为已接受
。
actualtext = driver.findElement(By.xpath("/html/body/div[3]/div/ng-include/div/div/div[1]/div/h3")).gettext();