Building the test framework:
test automation patterns - II
Test automation basics with Selenium & Java
By:
Evgeny Novikov
Email:
xevgnov@[Link]
Patterns: why and when?
Why you should know them
-> to understand different projects
Use it when
-> when it bring benefits: maintainability,
readability, etc
By:
Evgeny Novikov
Email:
xevgnov@[Link]
Driver Factory
method
By:
Evgeny Novikov
Email:
xevgnov@[Link]
Driver factory method pattern
It resolve problems:
● encapsulates driver
initialization and configuration
● handle cross browser testing
in test framework
WebDriver
By:
Evgeny Novikov
Email:
xevgnov@[Link]
Driver factory method: multiple browsers
public enum Browser {
CHROME,
IE,
EDGE,
FIREFOX,
SAFARI
} By:
Evgeny Novikov
Email:
xevgnov@[Link]
Driver factory method implementation
public class DriverFactory {
private static WebDriver driver;
private static final String DRIVER_PATH = "src/test/resources/";
public static WebDriver getDriver(Browser browser) {
File file;
switch (browser) {
case CHROME:
file = new File(DRIVER_PATH + "[Link]");
[Link]("[Link]", [Link]());
driver = new ChromeDriver();
break;
case IE:
file = new File(DRIVER_PATH + "[Link]");
[Link]("[Link]", [Link]());
driver = new InternetExplorerDriver();
break;
default: //add default browser here
} By:
[Link]().window().maximize(); Evgeny Novikov
return driver; Email:
} xevgnov@[Link]
}
Driver factory method usage
@BeforeClass
public void setUp() {
driver = [Link]([Link]));
[Link]("[Link]
steps = new SearchSteps();
}
The factory method,
which accepts
desired browser and
initializes the driver
By:
Evgeny Novikov
Email:
xevgnov@[Link]
Property reader
By:
Evgeny Novikov
Email:
xevgnov@[Link]
Property reader
It resolve problems:
● avoiding hard coded
properties
● reading system variables
● reading variables from file
By:
Evgeny Novikov
Email:
xevgnov@[Link]
Property reader implementation
public class PropertyReader {
public static String getBaseUrl() {
return getProperty("url");
}
public static Browser getBrowser() {
return [Link](getProperty("browser"));
}
private static String getProperty(String propertyName) {
if ([Link](propertyName) == null) {
return getPropertyFromFile(propertyName);
} else {
return [Link](propertyName);
}
}
By:
Evgeny Novikov
- to be continued on Email:
the next slide xevgnov@[Link]
private static String getPropertyFromFile(String propertyName) {
Properties prop = new Properties();
Property reader
InputStream input = null;
implementation
try {
input = new
FileInputStream("src/test/resources/[Link]");
- continuation
[Link](input);
} catch (IOException ex) {
[Link]("Cannot read property value for " +
propertyName);
[Link]();
} finally {
if (input != null) {
try {
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
return [Link](propertyName); By:
} Evgeny Novikov
} Email:
xevgnov@[Link]
Property reader usage
@BeforeClass
public void setUp() {
driver = [Link]([Link]());
[Link]([Link]());
steps = new SearchSteps();
Getting strings from properties using
By:
PropertyReader Evgeny Novikov
Email:
xevgnov@[Link]
Property reader usage: sources of properties
● read from property file
● read system variables
Run tests in console via maven:
mvn clean test -Dbrowser=CHROME -Durl=[Link]
By:
Evgeny Novikov
Email:
xevgnov@[Link]
Add new property to your test
framework
1. Add possibility to regulate the
option below from properties
2. [Link]().window().maximize();
3. This property should contains
Exercise just “true” or “false”.
Add new property For example: maximize=true
so if maximize is true, the browser
will be opened in the maximized
mode.