New article from seleniumjava.com:
https://seleniumjava.com/2017/06/05/how-to-create-page-objects-with-selenium-webdriver/
https://seleniumjava.com/2017/06/05/how-to-create-page-objects-with-selenium-webdriver/
@Name("SEARCH_HEADER")
@FindBy(className = "main-navigation-container")
public class SearchHeader extends HtmlElement{
@Name("SEARCH_FIELD")
@FindBy(id = "ctl00_MasterHeader_ctl00_uchead_GlobalSearchUC_TxtSearchKeyword")
private TextInput searchKeywordTxt;
@Name("SEARCH_BUTTON")
@FindBy(id = "ctl00_MasterHeader_ctl00_uchead_GlobalSearchUC_BtnSubmitSearch")
private Button searchBtn;
public void search(String keyword) {
searchKeywordTxt.click();
searchKeywordTxt.clear();
searchKeywordTxt.sendKeys(keyword);
searchBtn.click();
}
}
package com.bestbuy.demo.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD})
public @interface Name {
String value();
}
package com.bestbuy.demo.element;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
public class CheckBox extends TypifiedElement {
public CheckBox(WebElement wrappedElement) {
super(wrappedElement);
}
public WebElement getLabel() {
try {
return getWrappedElement().findElement(By.xpath("following-sibling::label"));
} catch (NoSuchElementException e) {
return null;
}
}
public String getLabelText() {
WebElement label = getLabel();
return label == null ? null : label.getText();
}
public String getText() {
return getLabelText();
}
public void select() {
if (!isSelected())
getWrappedElement().click();
}
public void deselect() {
if (isSelected())
getWrappedElement().click();
}
public void set(boolean value) {
if (value)
select();
else
deselect();
}
}
package com.bestbuy.demotests.testlisteners;
import java.lang.reflect.Field;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import com.bestbuy.demo.exceptions.HtmlElementsException;
import com.bestbuy.demo.utils.Driver.BrowserDriver;
import com.bestbuy.demo.utils.Driver.Screenshot;
import org.openqa.selenium.WebDriver;
import static com.bestbuy.demotests.BaseTest.BaseTestClass.*;
public class TestListener implements ITestListener {
@Override
public void onTestFailure(ITestResult result) {
try {
Screenshot screenshot =
new Screenshot(getDriverFromBaseTest(result));
screenshot.capture(result.getName());
}
catch (Exception ex) {
throw new HtmlElementsException(ex.getMessage());
}
}
@SuppressWarnings("unchecked")
private WebDriver getDriverFromBaseTest(ITestResult result)
throws IllegalAccessException {
WebDriver driver = null;
try {
Class< ? extends ITestResult> testClass =
(Class< ? extends ITestResult>) result.getInstance().getClass();
Class< ?extends ITestResult> baseTestClass =
(Class< ? extends ITestResult>) testClass.getSuperclass();
Field driverField = baseTestClass.getDeclaredField("driver");
driver = (BrowserDriver)driverField.get(result.getInstance());
return driver;
}
catch (SecurityException | NoSuchFieldException | IllegalArgumentException ex) {
throw new HtmlElementsException("error getting the driver from base test");
}
}
@Override
public void onTestSuccess(ITestResult result)
{}
@Override
public void onTestSkipped(ITestResult result)
{}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result)
{}
@Override
public void onStart(ITestContext context)
{}
@Override
public void onFinish(ITestContext context)
{}
@Override
public void onTestStart(ITestResult arg0)
{}
}
I have been a software manual tester for 10 years.
Due to the regression tests that I do every other week, it is imperative that I get this function automated to help me with the rest of my job.
I regression test over 90 custom sites for my company and really need to get this done by February 2017.
Please help.