[英]using general class for Selenium automation test with java
我真的是Selenium和Java自動化測試的新手,我想創建一個General類來存儲將在其他類中使用的方法,例如Login,單擊按鈕...在其他類中,我將測試打開的頁面,輸入用戶名,密碼,然后單擊某些元素。 我的代碼如下:GeneralMethods:
public class GeneralMethods {
String baseURL;
String chromepath =".../Webdriver/chromedriverv2.36";
public WebDriver driver;
@BeforeTest
public void openBrowser() {
System.setProperty("webdriver.chrome.driver", chromepath);
driver = new ChromeDriver();
driver.get(baseURL);
}
public void loginAccount(String username, String password) {
}
public void clickOnElement() {
//TODO: click on web element which other class will call
}
public void closeBrowser() {
driver.quit();
}
public void refreshBrowser() {
driver.findElement(By.xpath("//body")).sendKeys(Keys.F5);
}
該類登錄並在此處單擊一些元素:
public class LoginUser extends GeneralMethods {
String baseURL = "mydomain";
String username = "myusername123@gmail.com";
String userpassword = "mypassword123";
@BeforeTest
public void openBrowser() {
driver.get(baseURL);
}
@Test
public void loginEmployer() {
//TODO: Login an user with username and password
}
@Test
public void clickOnButton() {
//TODO: click on a button with Xpath
}
}
請幫助我糾正代碼,並建議我如何研究是否存在任何問題。
建議 :
您的所有實用程序(例如loginAccount,ClickOnElement,refreshBrowser)都應位於GeneralMethods類中。
您可以從GeneralMethods類擴展您的Test類。 (就像您已經完成的一樣。)
您的Utility類不需要包含任何TestNG批注。
測試方法 @Test public void clickOnButton(){}
在您的LoginUser中不能將其作為測試方法,因為單擊按鈕只是與WebElement的交互。
使用以下代碼: driver.navigate()。refresh();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.