[英]using general class for Selenium automation test with java
I am really new to automation test with Selenium and Java, I want to create a General class to store methods that will use in others class, like Login, Click a button... In other class, I will do test that open page, input username, password and click on some element. 我真的是Selenium和Java自动化测试的新手,我想创建一个General类来存储将在其他类中使用的方法,例如Login,单击按钮...在其他类中,我将测试打开的页面,输入用户名,密码,然后单击某些元素。 my code as below: GeneralMethods:
我的代码如下: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);
}
The class login and click on some element here: 该类登录并在此处单击一些元素:
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
}
} }
please help me correct the code an suggest me how to research if there any problem. 请帮助我纠正代码,并建议我如何研究是否存在任何问题。
Suggestion : 建议 :
Your all utilities like loginAccount , ClickOnElement , refreshBrowser , everything should be in GeneralMethods class. 您的所有实用程序(例如loginAccount,ClickOnElement,refreshBrowser)都应位于GeneralMethods类中。
You may extends your Test class from GeneralMethods class. 您可以从GeneralMethods类扩展您的Test类。 (Like you've already done.)
(就像您已经完成的一样。)
Your Utility class does not need to contain any TestNG annotations. 您的Utility类不需要包含任何TestNG批注。
Test Method @Test public void clickOnButton() { } 测试方法 @Test public void clickOnButton(){}
In your LoginUser can't be test method , as clicking on a button is just interaction with WebElement. 在您的LoginUser中不能将其作为测试方法,因为单击按钮只是与WebElement的交互。
use this code : driver.navigate().refresh(); 使用以下代码: driver.navigate()。refresh();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.