简体   繁体   English

使用通用类对Java进行Selenium自动化测试

[英]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 : 建议

  1. Your all utilities like loginAccount , ClickOnElement , refreshBrowser , everything should be in GeneralMethods class. 您的所有实用程序(例如loginAccount,ClickOnElement,refreshBrowser)都应位于GeneralMethods类中。

  2. You may extends your Test class from GeneralMethods class. 您可以从GeneralMethods类扩展您的Test类。 (Like you've already done.) (就像您已经完成的一样。)

  3. Your Utility class does not need to contain any TestNG annotations. 您的Utility类不需要包含任何TestNG批注。

  4. 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的交互。

  1. For your code : public void refreshBrowser () { driver.findElement(By.xpath("//body")).sendKeys(Keys.F5); 对于您的代码:public void refreshBrowser (){driver.findElement(By.xpath(“ // body”))。sendKeys(Keys.F5); } }

use this code : driver.navigate().refresh(); 使用以下代码: driver.navigate()。refresh();

  1. String baseURL instance field in your GeneralMethods class is not initialized, you may face runtime Exception while running the code. GeneralMethods类中的String baseURL实例字段未初始化,运行代码时可能会遇到运行时异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 测试自动化。 文件上传Java和Selenium - Test Automation. File upload Java and Selenium 使用Selenium WebAPI进行Jira测试自动化 - Jira Test Automation Using Selenium WebAPI 使用Selenium Webdriver的ExtJS测试自动化 - ExtJS Test Automation using Selenium Webdriver 结合使用Selenium和Java进行自动化测试 - Drag & Drop using Selenium with Java for automation testing 使用Jenkins和Selenium Java调度自动化任务 - Scheduling of Automation Tasks Using Jenkins With Selenium Java 使用selenium rc进行Flex自动化的Java代码 - java code for flex automation using selenium rc 使用 Selenium WebDriver/JAVA 自动化大型机作业 - Automation of Mainframe jobs using Selenium WebDriver/JAVA 无法使用 selenium (JAVA) 对带有 reCaptcha 的注册表单进行自动化测试 - Can't automation test for registration form with reCaptcha with selenium (JAVA) Selenium Automation:在JAVA中使用selenium识别唯一的webelements集 - Selenium Automation : Identifying unique set of webelements using selenium in JAVA Test Automation Using Java Selenium - Using JavaMail to check a confirmation email - Doesn't get mail if email arrives few seconds late - Test Automation Using Java Selenium - Using JavaMail to check a confirmation email - Doesn't get mail if email arrives few seconds late
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM