0% found this document useful (0 votes)
2 views14 pages

Building The Test Framework: Test Automation Patterns - II

The document discusses the implementation of a test automation framework using Selenium and Java, focusing on design patterns such as the Driver Factory method and Property Reader. It explains how these patterns enhance maintainability and readability while facilitating cross-browser testing and property management. Additionally, it provides code snippets and usage examples for initializing WebDriver and reading properties from files or system variables.

Uploaded by

sxoterry
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

Building The Test Framework: Test Automation Patterns - II

The document discusses the implementation of a test automation framework using Selenium and Java, focusing on design patterns such as the Driver Factory method and Property Reader. It explains how these patterns enhance maintainability and readability while facilitating cross-browser testing and property management. Additionally, it provides code snippets and usage examples for initializing WebDriver and reading properties from files or system variables.

Uploaded by

sxoterry
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

You might also like