我是Selenium和Java自动化测试的新手,我想创建一个General类来存储将在其他类中使用的方法,比如Login,Click a button ... 在其他课程中,我将测试打开的页面,输入用户名,密码并点击一些元素。 我的代码如下: 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
}
}
请帮我纠正代码,建议我如何研究是否有任何问题。
答案 0 :(得分:0)
建议:
您的所有实用程序如loginAccount,ClickOnElement,refreshBrowser,一切都应该在GeneralMethods类中。
您可以从GeneralMethods类扩展Test类。 (就像你已经完成的那样。)
您的Utility类不需要包含任何TestNG注释。
测试方法 @Test public void clickOnButton(){}
在您的LoginUser中不能是测试方法,因为单击按钮只是与WebElement的交互。
使用此代码: driver.navigate()。refresh();