Skip to content

Commit

Permalink
dont throw npe
Browse files Browse the repository at this point in the history
  • Loading branch information
ddavison committed May 20, 2015
1 parent 6c05188 commit a7c6ba6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<name>conductor</name>
<url>http://github.com/conductor-framework/conductor</url>
<version>2.0.0</version>
<version>2.0.1</version>
<description>A quick and easy start-up browser automation framework based on Selenium WebDriver</description>

<parent>
Expand Down
70 changes: 35 additions & 35 deletions src/main/java/io/ddavison/conductor/Locomotive.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public Locomotive() {
props.load(getClass().getResourceAsStream("/default.properties"));
} catch (IOException e) {
logFatal("Couldn't load in default properties");
}
} catch (Exception e) {}

/**
* Order of overrides:
Expand Down Expand Up @@ -190,7 +190,7 @@ public Class<? extends Annotation> annotationType() {
x.printStackTrace();
return;
}

actions = new Actions(driver);
driver.navigate().to(baseUrl);
}
Expand Down Expand Up @@ -232,19 +232,19 @@ static public String findFile(String filename) {
}
return "";
}

@After
public void teardown() {
driver.quit();
}

/**
* Private method that acts as an arbiter of implicit timeouts of sorts.. sort of like a Wait For Ajax method.
*/
public WebElement waitForElement(By by) {
int attempts = 0;
int size = driver.findElements(by).size();

while (size == 0) {
size = driver.findElements(by).size();
if (attempts == MAX_ATTEMPTS) fail(String.format("Could not find %s after %d seconds",
Expand All @@ -258,9 +258,9 @@ public WebElement waitForElement(By by) {
x.printStackTrace();
}
}

if (size > 1) System.err.println("WARN: There are more than 1 " + by.toString() + " 's!");

return driver.findElement(by);
}

Expand Down Expand Up @@ -316,12 +316,12 @@ public String getText(String css) {
public String getText(By by) {
String text;
WebElement e = waitForElement(by);

if (e.getTagName().equalsIgnoreCase("input") || e.getTagName().equalsIgnoreCase("select"))
text = e.getAttribute("value");
else
text = e.getText();

return text;
}

Expand Down Expand Up @@ -376,7 +376,7 @@ public Locomotive selectOptionByValue(By by, String value) {
box.selectByValue(value);
return this;
}

/* Window / Frame Switching */

public Locomotive waitForWindow(String regex) {
Expand All @@ -396,7 +396,7 @@ public Locomotive waitForWindow(String regex) {
else {
// try for title
m = p.matcher(driver.getTitle());

if (m.find()) {
attempts = 0;
return switchToWindow(regex);
Expand All @@ -405,7 +405,7 @@ public Locomotive waitForWindow(String regex) {
} catch(NoSuchWindowException e) {
if (attempts <= MAX_ATTEMPTS) {
attempts++;

try {Thread.sleep(1000);}catch(Exception x) { x.printStackTrace(); }

return waitForWindow(regex);
Expand All @@ -429,62 +429,62 @@ public Locomotive waitForWindow(String regex) {

public Locomotive switchToWindow(String regex) {
Set<String> windows = driver.getWindowHandles();

for (String window : windows) {
driver.switchTo().window(window);
System.out.println(String.format("#switchToWindow() : title=%s ; url=%s",
driver.getTitle(),
driver.getCurrentUrl()));

p = Pattern.compile(regex);
m = p.matcher(driver.getTitle());

if (m.find()) return this;
else {
m = p.matcher(driver.getCurrentUrl());
if (m.find()) return this;
}
}

fail("Could not switch to window with title / url: " + regex);
return this;
}

public Locomotive closeWindow(String regex) {
if (regex == null) {
driver.close();

if (driver.getWindowHandles().size() == 1)
driver.switchTo().window(driver.getWindowHandles().iterator().next());

return this;
}

Set<String> windows = driver.getWindowHandles();

for (String window : windows) {
try {
driver.switchTo().window(window);

p = Pattern.compile(regex);
m = p.matcher(driver.getTitle());

if (m.find()) {
switchToWindow(regex); // switch to the window, then close it.
driver.close();

if (windows.size() == 2) // just default back to the first window.
driver.switchTo().window(windows.iterator().next());
} else {
m = p.matcher(driver.getCurrentUrl());
if (m.find()) {
switchToWindow(regex);
driver.close();

if (windows.size() == 2) driver.switchTo().window(windows.iterator().next());
}
}

} catch(NoSuchWindowException e) {
fail("Cannot close a window that doesn't exist. ["+regex+"]");
}
Expand Down Expand Up @@ -518,9 +518,9 @@ public Locomotive switchToDefaultContent() {
driver.switchTo().defaultContent();
return this;
}

/* ************************ */

/* Validation Functions for Testing */

public Locomotive validatePresent(String css) {
Expand Down Expand Up @@ -549,7 +549,7 @@ public Locomotive validateText(String css, String text) {

public Locomotive validateText(By by, String text) {
String actual = getText(by);

assertTrue(String.format("Text does not match! [expected: %s] [actual: %s]", text, actual), text.equals(actual));
return this;
}
Expand Down Expand Up @@ -615,24 +615,24 @@ public Locomotive validateAttribute(By by, String attr, String regex) {
} catch (Exception x) {
fail("Cannot validate an attribute if an element doesn't have it!");
}

p = Pattern.compile(regex);
m = p.matcher(actual);
assertTrue(String.format("Attribute doesn't match! [Selector: %s] [Attribute: %s] [Desired value: %s] [Actual value: %s]",

assertTrue(String.format("Attribute doesn't match! [Selector: %s] [Attribute: %s] [Desired value: %s] [Actual value: %s]",
by.toString(),
attr,
regex,
actual
), m.find());

return this;
}

public Locomotive validateUrl(String regex) {
p = Pattern.compile(regex);
m = p.matcher(driver.getCurrentUrl());

assertTrue("Url does not match regex [" + regex + "] (actual is: \""+driver.getCurrentUrl()+"\")", m.find());
return this;
}
Expand All @@ -646,7 +646,7 @@ public Locomotive validateFalse(boolean condition) {
assertFalse(condition);
return this;
}

/* ================================ */

public Locomotive goBack() {
Expand All @@ -659,7 +659,7 @@ public Locomotive navigateTo(String url) {
if (url.contains("://")) driver.navigate().to(url);
else if (url.startsWith("/")) driver.navigate().to(baseUrl.concat(url));
else driver.navigate().to(driver.getCurrentUrl().concat(url));

return this;
}

Expand Down

0 comments on commit a7c6ba6

Please sign in to comment.