Skip to content
This repository has been archived by the owner on Nov 15, 2020. It is now read-only.

Properties sample #27

Merged
merged 12 commits into from
Feb 15, 2017
1 change: 0 additions & 1 deletion src/main/java/ru/sbtqa/tag/pagefactory/Page.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ru.sbtqa.tag.pagefactory;

import static java.lang.String.format;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/ru/sbtqa/tag/pagefactory/PageFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ public class PageFactory {
private static boolean aspectsDisabled = false;

private static final String ENVIRONMENT = Props.get("driver.environment");
private static final String INITIAL_URL = Props.get("driver.url");
private static final String PAGES_PACKAGE = Props.get("page.package");
private static final String TIMEOUT = Props.get("page.load.timeout");
private static final String BROWSER_NAME = Props.get("browser.name");
private static final String BROWSER_NAME = Props.get("webdriver.browser.name");

private static final String ENVIRONMENT_WEB = "web";
private static final String ENVIRONMENT_MOBILE = "mobile";
Expand Down Expand Up @@ -167,11 +166,4 @@ public static Environment getEnvironment() {
throw new FactoryRuntimeException("Environment '" + ENVIRONMENT + "' is not supported");
}
}

/**
* @return the INITIAL_URL
*/
public static String getInitialUrl() {
return INITIAL_URL;
}
}
18 changes: 2 additions & 16 deletions src/main/java/ru/sbtqa/tag/pagefactory/PageWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import ru.sbtqa.tag.pagefactory.annotations.PageEntry;
import ru.sbtqa.tag.pagefactory.exceptions.PageInitializationException;
import ru.sbtqa.tag.qautils.errors.AutotestError;
import ru.sbtqa.tag.qautils.properties.Props;

public class PageWrapper {

Expand Down Expand Up @@ -142,7 +141,8 @@ public Page changeUrlByTitle(String packageName, String title) throws PageInitia
} else {
try {
URL currentUrl = new URL(PageFactory.getWebDriver().getCurrentUrl());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current url is fetched as a URL instance, why not build it with the same class, instead of string concatenations and COLON_TWO_SLASHES variables? Or use https://docs.oracle.com/javaee/7/api/javax/ws/rs/core/UriBuilder.html or https://github.com/mikaelhg/urlbuilder for URL buildings

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And no, this is not a legacy. When one touches legacy shit and changes "//" string to a const, he gets involved. So one must make a good code from legacy shit, not a less shitty legacy shit.

String finalUrl = currentUrl.getProtocol() + "://" + currentUrl.getAuthority() + getUrlPrefix() + ((PageEntry) annotation).url();
String finalUrl = new URL(currentUrl.getProtocol(), currentUrl.getHost(), currentUrl.getPort(),
((PageEntry) annotation).url()).toString();
PageFactory.getWebDriver().navigate().to(finalUrl);
} catch (MalformedURLException ex) {
LOG.error("Failed to get current url", ex);
Expand All @@ -155,20 +155,6 @@ public Page changeUrlByTitle(String packageName, String title) throws PageInitia
throw new AutotestError("Page " + title + " doesn't have fast URL in PageEntry");
}

/**
*
* @return
*/
private String getUrlPrefix() {
String prefix = Props.get("webdriver.url.prefix");
if (!"".equals(prefix)) {
return ((prefix.startsWith("/")) ? "" : "/")
+ prefix
+ ((prefix.endsWith("/")) ? "" : "/");
}
return "/";
}

/**
*
* @param packageName TODO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public void doAroundClick(ProceedingJoinPoint joinPoint) throws Throwable {
}

String elementHighlightStyle = null;
boolean isVideoHighlightEnable = Boolean.valueOf(Props.get("video.highlight.enable"));
if (isVideoHighlightEnable) {
boolean isVideoHighlightEnabled = Boolean.valueOf(Props.get("video.highlight.enabled"));
if (isVideoHighlightEnabled) {
elementHighlightStyle = WebExtension.highlightElementOn(targetWebElement);
}

Expand Down Expand Up @@ -82,7 +82,7 @@ public void doAroundClick(ProceedingJoinPoint joinPoint) throws Throwable {
PageFactory.getInstance().getPage(elementRedirect);
}

if (isVideoHighlightEnable) {
if (isVideoHighlightEnabled) {
WebExtension.highlightElementOff(targetWebElement, elementHighlightStyle);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
Expand All @@ -21,14 +20,20 @@ public class TagMobileDriver {
private static final Logger LOG = LoggerFactory.getLogger(TagMobileDriver.class);

private static AppiumDriver<AndroidElement> mobileDriver;

private static final String APPIUM_URL = Props.get("appium.url");
private static final String APPIUM_DEVICE_NAME = Props.get("appium.device.name");
private static final String APPIUM_DEVICE_PLATFORM = Props.get("appium.device.platform");
private static final String APPIUM_APP_PACKAGE = Props.get("appium.app.package");
private static final String APPIUM_APP_ACTIVITY = Props.get("appium.app.activity");
private static final String VIDEO_ENABLED = Props.get("video.enabled", "false");

public static AppiumDriver<AndroidElement> getDriver() {
if (Environment.MOBILE != PageFactory.getEnvironment()) {
throw new FactoryRuntimeException("Failed to get mobile driver while environment is not mobile");
}

if (null == mobileDriver) {
if (Boolean.valueOf(Props.get("video.enable"))) {
if (Boolean.valueOf(VIDEO_ENABLED)) {
VideoRecorder.getInstance().startRecording();
}

Expand All @@ -39,20 +44,20 @@ public static AppiumDriver<AndroidElement> getDriver() {

private static void createDriver() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", Props.get("appium.device.name"));
capabilities.setCapability("platformVersion", Props.get("appium.device.platform"));
capabilities.setCapability("appPackage", Props.get("appium.app.package"));
capabilities.setCapability("appActivity", Props.get("appium.app.activity"));
capabilities.setCapability("deviceName", APPIUM_DEVICE_NAME);
capabilities.setCapability("platformVersion", APPIUM_DEVICE_PLATFORM);
capabilities.setCapability("appPackage", APPIUM_APP_PACKAGE);
capabilities.setCapability("appActivity", APPIUM_APP_ACTIVITY);
capabilities.setCapability("autoGrantPermissions", "true");
capabilities.setCapability("unicodeKeyboard", "true");
capabilities.setCapability("resetKeyboard", "true");
LOG.info("Capabilities are {}", capabilities);

URL url;
try {
url = new URL(PageFactory.getInitialUrl());
url = new URL(APPIUM_URL);
} catch (MalformedURLException e) {
throw new FactoryRuntimeException("Failed to connect to appium on url " + PageFactory.getInitialUrl(), e);
throw new FactoryRuntimeException("Could not parse appium url. Check 'appium.url' property", e);
}

setAspectsDisabled(true);
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/ru/sbtqa/tag/pagefactory/drivers/TagWebDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,27 @@ public class TagWebDriver {
private static final Logger log = LoggerFactory.getLogger(TagWebDriver.class);

private static WebDriver webDriver;
private static final int ATTEMPTS_TO_START_WEBDRIVER = Integer.parseInt(Props.get("driver.create.attempts", "3"));
private static BrowserMobProxy proxy;
private static final int WEBDRIVER_CREATE_ATTEMPTS = Integer.parseInt(Props.get("webdriver.create.attempts", "3"));
private static final String WEBDRIVER_PATH = "src/test/resources/webdrivers/";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move it to config and by default get it from system PATH

private static final String WEBDRIVER_URL = Props.get("webdriver.url");
private static final String WEBDRIVER_STARTING_URL = Props.get("webdriver.starting.url");
private static final String WEBDRIVER_PROXY_ENABLED = Props.get("webdriver.proxy.enabled", "false");
private static final String WEBDRIVER_BROWSER_IE_KILLONDISPOSE = Props.get("webdriver.browser.ie.killOnDispose", "false");
private static final String VIDEO_ENABLED = Props.get("video.enabled", "false");


public static org.openqa.selenium.WebDriver getDriver() {
if (Environment.WEB != PageFactory.getEnvironment()) {
throw new FactoryRuntimeException("Failed to get web driver while environment is not web");
}

if (null == webDriver) {
if (Boolean.valueOf(Props.get("video.enable"))) {
if (Boolean.valueOf(VIDEO_ENABLED)) {
VideoRecorder.getInstance().startRecording();
}

for (int i = 1; i <= ATTEMPTS_TO_START_WEBDRIVER; i++) {
for (int i = 1; i <= WEBDRIVER_CREATE_ATTEMPTS; i++) {
log.info("Attempt #" + i + " to start web driver");
try {
createDriver();
Expand All @@ -78,10 +84,10 @@ public static org.openqa.selenium.WebDriver getDriver() {
private static void createDriver() throws UnsupportedBrowserException {
DesiredCapabilities capabilities = new DesiredCapabilitiesParser().parse();

if (Props.get("webdriver.remote.host").isEmpty()) {
if (WEBDRIVER_URL.isEmpty()) {

//Local proxy available on local webdriver instances only
if (!Props.get("proxy.enable").isEmpty()) {
if (!WEBDRIVER_PROXY_ENABLED.isEmpty()) {
setProxy(new BrowserMobProxyServer());
proxy.start(0);
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
Expand Down Expand Up @@ -138,15 +144,15 @@ private static void createDriver() throws UnsupportedBrowserException {
throw new UnsupportedBrowserException("'" + PageFactory.getBrowserName() + "' is not supported yet");
}
try {
URL remoreUrl = new URL("http://" + Props.get("webdriver.remote.host") + ":4444/wd/hub");
setWebDriver(new RemoteWebDriver(remoreUrl, capabilities));
URL remoteUrl = new URL(WEBDRIVER_URL);
setWebDriver(new RemoteWebDriver(remoteUrl, capabilities));
} catch (MalformedURLException e) {
log.error("Can not parse remote url. Check webdriver.remote.host property");
log.error("Could not parse remote url. Check 'webdriver.url' property");
}
}
webDriver.manage().timeouts().pageLoadTimeout(getTimeOutInSeconds(), TimeUnit.SECONDS);
webDriver.manage().window().maximize();
webDriver.get(PageFactory.getInitialUrl());
webDriver.get(WEBDRIVER_STARTING_URL);
}

public static void dispose() {
Expand Down Expand Up @@ -178,7 +184,7 @@ public static void dispose() {

try {
if ("IE".equals(PageFactory.getBrowserName())
&& Boolean.parseBoolean(Props.get("browser.ie.killOnDispose", "true"))) {
&& Boolean.parseBoolean(WEBDRIVER_BROWSER_IE_KILLONDISPOSE)) {
// Kill IE by Windows means instead of webdriver.quit()
Runtime.getRuntime().exec("taskkill /f /im iexplore.exe").waitFor();
Runtime.getRuntime().exec("taskkill /f /im IEDriverServer.exe").waitFor();
Expand Down
37 changes: 37 additions & 0 deletions src/main/resources/config/application.properties-sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#mobile or web
driver.environment = mobile

#path to page objects
page.package = ru.sbtqa.tag.mobilefactory.entries
#default wait timeout in milliseconds
page.load.timeout = 60000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

milliseconds

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I need to fix?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment that it is in ms


video.enabled = false
video.path.dest = /tmp/video
video.path.temp = /tmp/video
video.highlight.enabled = false

#driver or raw
screenshot.strategy = driver

#tasks to kill before test (ONLY FOR WINDOWS)
tasks.to.kill = iexplorer,chrome
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it OS independent?

Copy link
Contributor Author

@kosteman kosteman Feb 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not. check this issue #26

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add in commet please whait is for windows only


#IN CASE OF MOBILE
#parameters for mobile driver create
appium.url = http://127.0.0.1:4723/wd/hub
appium.device.name = Android Emulator
appium.device.platform = 6.0
appium.app.package = com.android.settings
appium.app.activity = .Settings

#IN CASE OF WEB
#parameters for web driver create
webdriver.browser.name = Chrome
#kill ie browser process after test (ONLY FOR WINDOWS)
webdriver.browser.ie.killOnDispose = true
webdriver.starter.url = "http://google.com"
webdriver.create.attempts = 3
webdriver.proxy.enabled = false
#optional. In case of using remote WebDriver
webdriver.url = http://127.0.0.1:4444/wd/hub