0
我做硒數據驅動的框架,在Java語言硒TestNG的爪哇 - 參數太多
用TestNG我pageObject
Login_Page.java
存儲頁面上可用的所有定位器。
然後我有appModules
Login_Action.java
作爲重複登錄流程的常用功能。
如您所注意到的,步驟ExecuteLoginAction()
是相同的,但由於需要測試用例輸入而具有不同數量的參數。如何在這種情況下優化代碼?
正如我在測試腳本中,我會打電話給Login_Action.ExecuteLoginAction(many...parameters)
我怎樣才能避免這種MyTestScript_001Test()
長參數列表和Login_Action.ExecuteLoginAction()
pageObjects Login_Page.java
package pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class Login_Page extends BaseClass {
private static WebElement element = null;
public Login_Page(WebDriver driver){
super(driver);
}
public static WebElement txt_enterUsername() throws Exception{
try{
element = driver.findElement(By.name("username"));
}catch (Exception e){
throw(e);
}
return element;
}
public static WebElement txt_enterPassword() throws Exception{
try{
element = driver.findElement(By.name("password"));
}catch (Exception e){
throw(e);
}
return element;
}
public static WebElement btn_clickLoginBtn() throws Exception{
try{
element = driver.findElement(By.name("loginBtn"));
}catch (Exception e){
throw(e);
}
return element;
}
}
appModules Login_Action.java
package appModules;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.testng.Reporter;
import pageObjects.Login_Page;
public class Login_Action {
public static void ExecuteLoginAction(WebDriver driver, String ColTestCaseName, String ColUsername, String ColPassword,
String ColFirstName, String ColLastName, String ColAddress, String ColCountry, String ColGender) throws Exception{
// Click Login link
Home_Page.lnk_clickLoginBtn().click();
// Enter text for Username
Login_Page.txt_enterUsername().sendKeys(ColUsername);
// Enter text for Password
Login_Page.txt_enterPassword().sendKeys(ColPassword);
// Click Login submit button
Login_Page.btn_clickLoginSubmitBtn().click();
}
public static void ExecuteLoginAction(WebDriver driver, String ColTestCaseName, String ColUsername, String ColPassword,
String ColFirstName, String ColLastName, String ColAddress) throws Exception{
// Click Login link
Home_Page.lnk_clickLoginBtn().click();
// Enter text for Username
Login_Page.txt_enterUsername().sendKeys(ColUsername);
// Enter text for Password
Login_Page.txt_enterPassword().sendKeys(ColPassword);
// Click Login submit button
Login_Page.btn_clickLoginSubmitBtn().click();
}
}
Main Test Script (MyTestScript_001)
@Test(dataProvider="MyTestScript_001Data")
public void MyTestScript_001Test(String ColTestCaseName, String ColUsername, String ColPassword,
String ColFirstName, String ColLastName, String ColAddress) throws Exception{
// Login to web application
Login_Action.ExecuteLoginAction(driver, ColTestCaseName, ColUsername, ColPassword,
ColFirstName, ColLastName, ColAddress, ColCountry, ColGender);
// Enter First Name
UpdateProfile_Page.txtbx_enterFirstName().sendKeys(ColFirstName);
// Enter Last Name
UpdateProfile_Page.txtbx_enterLastName().sendKeys(ColLastName);
Search_Action.ExecuteSearchAction(driver, ColTestCaseName, ColUsername, ColPassword,
ColFirstName, ColLastName, ColAddress, ColCountry, ColGender);
Main Test Script (MyTestScript_002)
Main Test Script (MyTestScript_002)
@Test(dataProvider="MyTestScript_002Data")
public void MyTestScript_001Test(String ColTestCaseName, String ColUsername, String ColPassword,
String ColFirstName, String ColLastName, String ColAddress) throws Exception{
// Login to web application
Login_Action.ExecuteLoginAction(driver, ColTestCaseName, ColUsername, ColPassword,
ColFirstName, ColLastName, ColAddress);
// Enter text for Address
UpdateProfile_Page.txtbx_enterAddress().sendKeys(ColAddress);
您的建議都非常感謝!
如果executelogin動作只需要用戶名和密碼,你爲什麼在傳遞這麼多的數據。只需傳入用戶名和密碼。 – 2015-02-11 13:11:51
@niharika_neo,我的測試不僅適用於登錄部分,我只是最小化代碼以顯示在問題中。事實上,我的參數現在高達20 ++。謝謝 – christo 2015-02-11 14:31:08
製作對象而不是傳遞字符串。例如。創建具有名稱,密碼,地址等的用戶類 – 2015-02-12 06:16:04