I have divided the system modules into several packages in eclipse. In there one java class is for one particular page and the other consists of the test cases.
Apart from this, I have used the default package for common methods that are using throughout the whole system.
systemDefaultCommands(package name) > TestCommands.java
public class TestCommands {
public void click(By locator , WebDriver driver) {
try {
WebElement element = driver.findElement(locator);
element.click();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void type(By locator , WebDriver driver , String key) {
try {
WebElement element = driver.findElement(locator);
element.clear();
element.sendKeys(key);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
systemAdministratorfunctionalities > AdministratorPage
can we capture web elements as this?
public By userName() { return By.xpath("//input[@id='mat-input-0']"); } Is this correct ?
public class AdministratorPage extends TestCommands{
//web elements
public By userName() {
return By.xpath("//input[@id='mat-input-0']");
}
public By password() {
return By.xpath("//input[@id='mat-input-1']");
}
public By loginButton() {
return By.xpath("//span[@class='mat-button-wrapper']");
}
//Business Functions
public void openApplication(String url,WebDriver driver) {
open(url, driver);
}
public void loginToApplication(WebDriver driver,String userName,String password){
type(userName(),driver,userName);
type(password(),driver,password);
click(loginButton(),driver);
}
}
systemAdministratorfunctionalities > TC_Administrator This is where the actions performing and print the relevant results by TestCommands methods. I have used xml file to send browser name and the data container as data provider.
public class TC_Administrator {
WebDriver driver;
String baseUrl;
AdministratorPage admin = new AdministratorPage();
@Parameters("browser")
@BeforeMethod
public void beforeMethod(String browser) {
if(browser.equals("firefox")) {
System.setProperty("webdriver.gecko.driver", "drivers\\\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}else if(browser.equals("chrome")){
System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
}
@Test(priority = 1,dataProvider = "DataContainer")
public void openAdministrator(String userName, String password,String searchCustomer) throws InterruptedException {
admin.openApplication("https://test.com/", driver);
admin.loginToApplication(driver, userName, password);
}
@AfterMethod
public void afterMethod() {
driver.quit();
}