我正在尝试为登录测试脚本创建一个函数,该脚本传递用户名和密码等参数。我是新手,请详细说明。这是我写的代码 -
public void portalLogin(String Username, String Password){
driver.get(URL);
driver.findElement(By.id("UserName")).clear();
driver.findElement(By.id("UserName")).sendKeys(Username);
driver.findElement(By.id("Password")).clear();
driver.findElement(By.id("Password")).sendKeys(Password);
driver.findElement(By.cssSelector("input.submitBtn")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
我只想将这个奇怪的函数称为另一个测试类,这样我就不必一遍又一遍地写这个。请帮忙!
答案 0 :(得分:3)
你说你试图使用页面对象模式。以下是我认为您正在寻找的内容:
public class LogonPage {
@FindBy(id="UserName")
public WebElement userNameField;
@FindBy(id = "Password")
public WebElement passwordField;
@FindBy(css = "input.submitBtn")
public WebElement submitButton;
public LogonPage(WebDriver driver){
PageFactory.initElements(driver, this);
}
public void portalLogin(String username, String password){ //Only call this when you are already on the logon page
userNameField.clear();
userNameField.sendKeys(username);
passwordField.clear();
passwordField.sendKeys(password);
submitButton.click();
//Waiting should also be done after the function
}
}
然后,您必须在主代码中执行此操作:
LogonPage page = new LogonPage(driver);
page.doLogin(username, password);
答案 1 :(得分:0)
设置超时并首先导航到URL。在应用程序中打开登录页面。
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(URL);
public void doLogin(String Username, String Password)
{
driver.findElement(By.id("UserName")).clear();
driver.findElement(By.id("UserName")).sendKeys(Username);
driver.findElement(By.id("Password")).clear();
driver.findElement(By.id("Password")).sendKeys(Password);
driver.findElement(By.cssSelector("input.submitBtn")).click();
}
如果您需要更多帮助,请告诉我......
答案 2 :(得分:0)
以下是JUnit参数化测试的示例。我认为这就是你要求的。
@RunWith(Parameterized.class)
public class DataProviderTest extends WebDriverUtils {
private static String testName, searchString, ddMatch;
public DataProviderTest( String tName, String sString, String dMatch ) {
testName = tName;
searchString = sString;
ddMatch = dMatch;
}
@Before
public void setUp() {
System.out.println("setUp");
}
@Parameters(name = "{0}: {1}: {2}")
public static List<String[]> loadParams() {
File tFile = loadGradleResource( System.getProperty("user.dir") + separator + "build" +
separator + "resources" + separator + "test" + separator + "testdata2.csv" );
List<String[]> rows = null;
if ( tFile.exists() ) {
CSVReader reader = null;
try {
reader = new CSVReader( new FileReader( tFile ), ',' );
rows = reader.readAll();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//String[][] csvMatrix = rows.toArray(new String[rows.size()][]);
System.out.println("Finished loadParams()");
return rows;
}
@Test
public void testParams() {
System.out.println("Param '{}' being run...", testName );
System.out.println("Search string: " + searchString );
System.out.println("ddMatch: " + ddMatch );
System.out.println("Test '{}' is done.", testName );
}
@After
public void cleanUp() {
System.out.println("Finished cleanUp");
}
}
答案 3 :(得分:0)
public static WebDriver LoginPage(String URL, CharSequence UName, CharSequence Pwd) throws Exception
{
driver.get(URL);
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys(UName);
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys(Pwd);
// [click on link or button after which popup window will appear. ]
driver.findElement(By.name("login")).click();
// System.out.println("URL of the page before - switchingTo: " + driver.getCurrentUrl());
Thread.sleep(2000);
//Switch to newly opened window and get the page titele
for(String CurrentWindowHandle : driver.getWindowHandles())
{ driver.switchTo().window(CurrentWindowHandle);
}
System.out.println("URL of the page after - switchingTo: " + driver.getCurrentUrl());
Thread.sleep(3000);
return driver;
}
然后在Driver脚本中,它可以被称为 LoginPage(vURL,vUserName,vPassword);