首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Selenium自动化登录页面测试用例

使用Selenium自动化登录页面测试用例
EN

Stack Overflow用户
提问于 2022-01-04 16:45:18
回答 1查看 898关注 0票数 0

由于我刚接触Selenium和Java,所以在测试登录页面时被卡住了。下面是我的代码,我正在尝试测试,它总是返回Test case failed,即使我给出了正确的用户名和密码。我想知道哪里出了问题。在检查相同条件时,URL也是正确的。我已采取的网址后登录到网站。

代码语言:javascript
复制
package Seleniumtesting;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Selenium {

    public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver","C:\\Users\\hp\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe"); 
    WebDriver d = new ChromeDriver();
    d.get("https://onelogin.adityabirlacapital.com/login");
    d.findElement(By.id("login-id")).sendKeys("my_username");
    d.findElement(By.id("password")).sendKeys("my_password");
    //d.findElement(By.xpath("//*[@id=\"password-login\"]")).click();
    d.findElement(By.id("password-login")).click();
    String u = d.getCurrentUrl();
    if(u.equalsIgnoreCase("https://onelogin.adityabirlacapital.com/my-dashboard"))
    {
        System.out.println("Test case passed");
    }
    else
    {
        System.out.println("Test case failed");
    }
        d.close();
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-05 07:09:48

您可以尝试在执行d.getCurrentUrl()之前添加延迟。在您的代码中添加一些类似TimeUnit.SECONDS.sleep(2);的内容,以确保在单击登录后,所有内容都已加载,检索当前URL的值。

其他解决方案可能是,在通过Selenium单击登录按钮之后,在主页中找到任何可能指示用户成功登录的元素,例如欢迎消息,或者如果已经有注销按钮。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70582206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档