如何处理硒中的多用户登录



我有一个网站,一个用户登录并提交一些东西,然后它移动到具有单独用户名/密码的"用户二",然后移动到"第三",最后到"第四"。因此,我必须创建一个脚本,如果第一个用户完成提交,则脚本应使用用户 2 登录,然后使用用户 3 登录,以便他们也可以提交。

我很困惑如何创建脚本以使多用户登录成为可能。此外,我将使用POM(页面对象模型(来创建脚本。

谢谢。

尝试多个用户电子邮件ID和密码的2D数组列表,试试这个:

1.使用网页驱动程序输入网址

    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.test.com/");

2.声明数组。

字符串 arr[][]= { {"user1@test.com","user2@test.com"} , {"Test@123","Test@123"} };

3.然后编写代码并注销当前登录用户:-

for(int i=0; i<arr.length-1 ; i++){
for(int j=0;j<arr.length;j++) { 
//Find Login button     
driver.findElement(By.xpath("xpath")).click();
driver.findElement(By.xpath("//*[@id="username"]")).sendKeys(arr[i][j]);
driver.findElement(By.xpath("//*[@id="password"]")).sendKeys(arr[i+1][j]);
//Click on Submit button
driver.findElement(By.xpath("//*[@id="submit"]")).click();
//Add your code
//Log out
driver.findElement(By.xpath("xpath")).click();           
        }    
    }

这个问题的另一个逻辑是,我们可以只使用一个 for 循环来运行程序

字符串 arr[][]= { {"user1@test.com","Test@123"} , {"user2@test.com","Test@123"} };

for(int i=0;i<arr.length;i++)
{

driver.findElement(By.xpath("xpath")).click();       
driver.findElement(By.xpath("//*[@id="username"]")).sendKeys(arr[i][0]);
driver.findElement(By.xpath("//*[@id="password"]")).sendKeys(arr[i][1]);
//Click on Submit button
driver.findElement(By.xpath("//*[@id="submit"]")).click();
//Add your code        
//Log out
driver.findElement(By.xpath("xpath")).click(); 
}   

试试这个方式:使用所有用户 ID 和密码创建 2D 数组列表,然后在两个循环内添加您的流程命令1. 登录2. 提交内容3.最后命令注销此用户。

例:

String arr[][]= { {"user1@abc.com","user2@abc.com"} , {"password1","password2"} }
for(i=0; i<2 ; i++){
 for(j=0; j<1 ; j++){
// Your logic here for login, submitting and logout
}
}

最新更新