Selenium Web驱动程序Java-始终使用用户和密码



我有很多代码是为用户和密码编写的,但我想要一种方法来声明和调用所有代码。

这是存在的吗?

public class Cadastro_Produto_ERP4ME {
@Test
public void iniciar_cadastro() throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://forme-hml.alterdata.com.br/");
Thread.sleep(1000);
driver.findElement(By.id("email-login")).sendKeys("user.dsn.shop"); **<<<<<<<<**
driver.findElement(By.xpath("//form/div[2]/div/input")).sendKeys("senha"); **<<<<<<<<**

如果我理解正确,您需要循环一组凭据。

用户名和密码存储为Map<String, String>对象的示例。

您可以循环一个驱动程序实例中的所有条目,也可以为每个条目创建新的驱动程序实例。

package selenium;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Cadastro_Produto_ERP4ME {
public static Map<String, String> credentialsMap = credentialsMap(); 

public static Map<String, String> credentialsMap() {
Map<String, String> credentialsMap = new HashMap<String, String>();
credentialsMap.put("username1", "password1");
credentialsMap.put("username2", "password2");
// etc.
return credentialsMap;
}

public void iniciar_cadastro() throws InterruptedException {
for (Map.Entry<String, String> entry: credentialsMap.entrySet()) {
// new driver for each entry
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://forme-hml.alterdata.com.br/");
Thread.sleep(1000);
driver.findElement(By.id("email-login")).sendKeys(entry.getKey());
driver.findElement(By.xpath("//form/div[2]/div/input")).sendKeys(entry.getValue());
// continue to login
}
}

public void iniciar_cadastro2() throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://forme-hml.alterdata.com.br/");
for (Map.Entry<String, String> entry: credentialsMap.entrySet()) {
// loop though map in one window
Thread.sleep(1000);
driver.findElement(By.id("email-login")).sendKeys(entry.getKey());
driver.findElement(By.xpath("//form/div[2]/div/input")).sendKeys(entry.getValue());
// reload login page or clear login form
}
}

}

credentialsMap可以作为参数使用。

最新更新