Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updates and fixed for issues found during TATTs PoC usage
* ControlBase logging fixes
* ControlBase added Selenium getCSSValue for Controls and member elements
* HTMLElement logging fixes
* HTMLElement added Selenium getCSSValue for Controlium HTMLElements
* SeleniumDriver added logging of Selenium Driver hash code during launch to help debug Selenium startup issues
* SeleniumDriver increased ability to get Selenium controlled browser windows
* SeleniumDriver changed some logging levels to make messages more consistent
  • Loading branch information
v-mwalk committed Oct 29, 2018
commit 5e95b56ba7f7bff39aa5da99e23cd157dd462ede
43 changes: 40 additions & 3 deletions src/main/java/TeamControlium/Controlium/ControlBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ public static <T extends ControlBase> T setControl(SeleniumDriver seleniumDriver
StopWatch timeWaited = StopWatch.createStarted();

try {
Logger.WriteLine(Logger.LogLevels.TestInformation, "Setting on Control [%s] from Parent [%s]",
Logger.WriteLine(Logger.LogLevels.TestInformation, "Setting on Control [%s] from Parent [%s] with SeleniumDriver hash [%s] (WebDriver hash [%s])",
newControl.getMapping() == null ? "No mapping logic!" : newControl.getMapping().getFriendlyName(),
parentControl == null ? "No parent Control - So Top Level control" : parentControl.getMapping() == null ? "No mapping logic!" : parentControl.getMapping().getFriendlyName());

parentControl == null ? "No parent Control - So Top Level control" : parentControl.getMapping() == null ? "No mapping logic!" : parentControl.getMapping().getFriendlyName(),
seleniumDriver==null?"null":Integer.toString(seleniumDriver.hashCode()),
seleniumDriver==null?"N/A":(seleniumDriver.getWebDriver()==null?"Null!":Integer.toString(seleniumDriver.getWebDriver().hashCode())));
//
// Check if ParentControl has become stale (has been redrawn). If so, refresh it (force a new findElement on it). Note that this
// will effectively ripple up to the top level
Expand Down Expand Up @@ -248,6 +249,42 @@ public boolean hasAttribute(String attributeName) {
}
}

public String getCssValue(ObjectMapping mapping, String valueName) {
HTMLElement element = findElement(mapping);
try {
return element.getCssValue(valueName);
}
catch (Exception e) {
return "";
}
}
public String getCssValue(String valueName) {
try {
return getRootElement().getCssValue(valueName);
}
catch (Exception e) {
return "";
}
}

public boolean hasCssValue(ObjectMapping mapping, String valueName) {
HTMLElement element = findElement(mapping);
try {
return element.hasCssValue(valueName);
}
catch (Exception e) {
return false;
}
}
public boolean hasCssValue(String valueName) {
try {
return getRootElement().hasCssValue(valueName);
}
catch (Exception e) {
return false;
}
}

public String getText(ObjectMapping mapping) {
HTMLElement element = findElement(mapping);
try {
Expand Down
33 changes: 31 additions & 2 deletions src/main/java/TeamControlium/Controlium/HTMLElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ public String getAttribute(String attribute) {
throw ex;
}
catch (Exception e) {
Logger.WriteLine(Logger.LogLevels.TestInformation,"Error thrown getting attribute [%s] from [%s]: %s",getFriendlyName(),e.getMessage());
Logger.WriteLine(Logger.LogLevels.TestInformation,"Error thrown getting attribute [%s] from [%s]: %s",attribute,getFriendlyName(),e.getMessage());
throw new RuntimeException(String.format("Error thrown getting attribute [%s] from [%s] ([%s])",attribute==null?"Null":attribute,getFriendlyName(),getMappingDetails().getActualFindLogic()),e);
}
}
Expand All @@ -570,11 +570,40 @@ public boolean hasAttribute(String attribute) {
throw ex;
}
catch (Exception e) {
Logger.WriteLine(Logger.LogLevels.TestDebug,"Error thrown getting attribute [%s] from [%s]. Assume does not have attribute: %s",getFriendlyName(),e.getMessage());
Logger.WriteLine(Logger.LogLevels.TestDebug,"Error thrown getting attribute [%s] from [%s]. Assume does not have attribute: %s",attribute,getFriendlyName(),e.getMessage());
return false;
}
}

public String getCssValue(String valueName) {
throwIfUnbound();
try {
return getSeleniumDriver().getCssValue(getUnderlyingWebElement(),valueName);
}
catch (InvalidElementState ex) {
throw ex;
}
catch (Exception e) {
Logger.WriteLine(Logger.LogLevels.TestInformation,"Error thrown getting CSS value [%s] from [%s]: %s",valueName,getFriendlyName(),e.getMessage());
throw new RuntimeException(String.format("Error thrown getting CSS value [%s] from [%s] ([%s])",valueName==null?"Null":valueName,getFriendlyName(),getMappingDetails().getActualFindLogic()),e);
}
}

public boolean hasCssValue(String valueName) {
throwIfUnbound();
try {
return getSeleniumDriver().hasCssValue(getUnderlyingWebElement(),valueName);
}
catch (InvalidElementState ex) {
throw ex;
}
catch (Exception e) {
Logger.WriteLine(Logger.LogLevels.TestDebug,"Error thrown getting CSS value [%s] from [%s]. Assume does not have CSS value: %s",valueName,getFriendlyName(),e.getMessage());
return false;
}
}


public void click() {
throwIfUnbound();
try {
Expand Down
71 changes: 66 additions & 5 deletions src/main/java/TeamControlium/Controlium/SeleniumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
Expand All @@ -28,6 +29,7 @@
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerDriverService;
import org.openqa.selenium.ie.InternetExplorerOptions;
import org.openqa.selenium.internal.WrapsDriver;
import org.w3c.dom.NodeList;

import javax.swing.text.html.HTMLDocument;
Expand Down Expand Up @@ -133,6 +135,9 @@ private void commonConstructs(boolean killFirst, String seleniumHost,String devi
setSeleniumHost(seleniumHost);

startOrConnectToSeleniumServer(killFirst);
Logger.WriteLine(Logger.LogLevels.FrameworkInformation,"Started SeleniumDriver. Hash code [%d]. WebDriver hash code [%s]",
this.hashCode(),
getWebDriver()==null?"null!":Integer.toString(getWebDriver().hashCode()));
}


Expand All @@ -158,6 +163,24 @@ public Duration setPageLoadTimeout(Duration pageLoadTimeout) {
public Browsers getDevice() { return _browser; }
public Browsers setDevice(Devices device) { _device=device; return getDevice();}

public Set<String> getAllBrowserWindows() {
if (getWebDriver()!=null) {
return getWebDriver().getWindowHandles();
}
else {
throw new RuntimeException("Cannot get Browser windows handles as WebDriver null!");
}
}

public void setBrowserWindow(String handle) {
if (getWebDriver()!=null) {
getWebDriver().switchTo().window(handle);
}
else {
throw new RuntimeException("Cannot set Browser window as WebDriver null!");
}
}

public void setIFrame(HTMLElement htmlElement) {
WebElement webElement = ((WebElement)htmlElement.getUnderlyingWebElement());
String tag = webElement.getTagName();
Expand Down Expand Up @@ -276,7 +299,7 @@ public HTMLElement findElement(HTMLElement parentElement,ObjectMapping objectMap
objectMapping.getFriendlyName(),
(parentElement == null) ? "DOM Top Level" : parentElement.getMappingDetails().getFriendlyName(),
timer.getTime());
Logger.WriteLine(Logger.LogLevels.Error, errorText);
Logger.WriteLine(Logger.LogLevels.FrameworkInformation, errorText);
throw new RuntimeException(errorText);

}
Expand Down Expand Up @@ -351,7 +374,7 @@ public List<HTMLElement> findElements(HTMLElement parentElement, ObjectMapping m
throw new RuntimeException("SeleniumDriver.FindElements called with mapping null!");
}

Logger.WriteLine(Logger.LogLevels.FrameworkDebug,"ObjectMapping = [%s] (%s)",mapping.getOriginalFindLogic(),mapping.getFriendlyName());
Logger.WriteLine(Logger.LogLevels.FrameworkDebug,"webDriver hash [%s], ObjectMapping = [%s] (%s)",webDriver==null?"Null!":Integer.toString(webDriver.hashCode()),mapping.getOriginalFindLogic(),mapping.getFriendlyName());

By seleniumFindBy = mapping.getSeleniumBy();

Expand Down Expand Up @@ -403,6 +426,19 @@ public List<HTMLElement> findElements(HTMLElement parentElement, ObjectMapping m
return returnElements;
}

public WebDriver getWebDriver() { return webDriver;}
public void setBrowserSize(int x, int y) {
if (webDriver==null) {
throw new RuntimeException("Selenium web driver has not been started!");
}
webDriver.manage().window().setSize(new Dimension(x,y));
}
public void setBrowserFullScreen() {
if (webDriver==null) {
throw new RuntimeException("Selenium web driver has not been started!");
}
webDriver.manage().window().fullscreen();
}

public void gotoURL(String fullURLPath) {
try {
Expand All @@ -414,7 +450,6 @@ public void gotoURL(String fullURLPath) {
throw e;
}
}

public String getPageTitle() {
try {
String title = webDriver.getTitle();
Expand Down Expand Up @@ -563,6 +598,7 @@ public void click(Object webElement) {
if (webElement == null) throw new RuntimeException("webElement null!");

try {
Logger.WriteLine(Logger.LogLevels.FrameworkDebug,"Click using WebDriver hash [%s]",((WrapsDriver) webElement).getWrappedDriver()==null?"null!!":Integer.toString(((WrapsDriver) webElement).getWrappedDriver().hashCode()));
((WebElement) webElement).click();
} catch (WebDriverException wde) {
String errorMessage = wde.getMessage();
Expand Down Expand Up @@ -632,6 +668,29 @@ public boolean hasAttribute(Object webElement,String attribute) {
}
}

public String getCssValue(Object webElement,String valueName) {
if (webElement==null) throw new RuntimeException("webElement null!");

try {
String cssValue = ((WebElement)webElement).getCssValue(valueName);
Logger.WriteLine(Logger.LogLevels.FrameworkDebug, "Got CSS Value [%s]: [%s]", valueName,(cssValue==null)?"Null":cssValue);
return (cssValue==null)?"":cssValue;

}
catch (InvalidElementStateException e) {
//
// Usually thrown by Selenium when element stale
//
throw new InvalidElementState(String.format("Unable to get element CSS Value [%s]. See underlying cause.",(valueName==null)?"Null!!":valueName),e);
}
}
public boolean hasCssValue(Object webElement,String valueName) {
if (webElement==null) throw new RuntimeException("webElement null!");

return (!getCssValue(webElement,valueName).isEmpty());
}


public void scrollIntoView(Object webElement) {
Logger.WriteLine(Logger.LogLevels.FrameworkDebug, "Scrolling element in to view using JavaScript injection - [Element].scrollIntoView()");
executeJavaScriptNoReturnData("arguments[0].scrollIntoView();", webElement);
Expand Down Expand Up @@ -856,8 +915,6 @@ else if (Browsers.isChrome()) {
String executable = "ChromeDriver.exe";
ChromeOptions options = new ChromeOptions();



setPathToDriverIfExistsAndIsExecutable(seleniumServerFolder, ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY,executable);
if (seleniumDebugMode) System.setProperty(ChromeDriverService.CHROME_DRIVER_VERBOSE_LOG_PROPERTY, "true");

Expand All @@ -870,6 +927,7 @@ else if (Browsers.isChrome()) {

if (killFirst) killAllProcesses(executable);
webDriver = new ChromeDriver(ChromeDriverService.createDefaultService(), options);
Logger.WriteLine(Logger.LogLevels.FrameworkDebug,"Chrome driver created. Hash [%d]",webDriver.hashCode());
}
else if (Browsers.isEdge()) {
String executable = "EdgeDriver.exe";
Expand All @@ -893,6 +951,7 @@ else if (Browsers.isEdge()) {
} catch (Exception e) {
throw new RuntimeException(String.format("Error instantiating [%s] (%s)", Browsers.isChrome() ? "Chrome" : Browsers.isEdge() ? "Edge" : Browsers.isInternetExplorer() ? "Internet Explorer" : Browsers.isSafari() ? "Safari" : "UNKNOWN!", seleniumServerFolder));
}

}

private List<HTMLElement> getHtmlElements(HTMLElement parentElement, ObjectMapping objectMapping, boolean allowMultipleMatches, boolean waitUntilSingle, boolean showMultiFound, long totalTimeoutMillis, long pollIntervalMillis, StopWatch timer) {
Expand Down Expand Up @@ -922,6 +981,8 @@ private void setPathToDriverIfExistsAndIsExecutable(final String pathToDriver, f
if (driver.exists() && driver.canExecute()) {
System.setProperty(driverExeProperty, driver.getAbsolutePath());
} else {


throw new IllegalArgumentException(String.format("Driver not found or is not executable in %s", pathToDriver));
}
}
Expand Down