Created
June 19, 2015 18:21
-
-
Save tjmaher/4d5c3a523cca6d79b8e7 to your computer and use it in GitHub Desktop.
List of common utilities used for Selenium / Java tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package utils; | |
| import org.openqa.selenium.*; | |
| import org.openqa.selenium.NoSuchElementException; | |
| import org.openqa.selenium.interactions.Actions; | |
| import org.openqa.selenium.support.ui.ExpectedConditions; | |
| import org.openqa.selenium.support.ui.WebDriverWait; | |
| import org.testng.TestException; | |
| public abstract class CommonUtils { | |
| public WebDriver _driver; | |
| public WebDriverWait wait; | |
| public Actions actions; | |
| public CommonUtils() { | |
| _driver = DriverUtils.getFirefoxDriver(); | |
| } | |
| public void navigateToURL(String URL) { | |
| System.out.println("Navigating to: " + URL); | |
| System.out.println("Thread id = " + Thread.currentThread().getId()); | |
| try { | |
| _driver.navigate().to(URL); | |
| } catch (Exception e) { | |
| System.out.println("URL did not load: " + URL); | |
| throw new TestException("URL did not load"); | |
| } | |
| } | |
| public String getPageTitle() { | |
| try { | |
| System.out.print(String.format("The title of the page is: %s\n\n", _driver.getTitle())); | |
| return _driver.getTitle(); | |
| } catch (Exception e) { | |
| throw new TestException(String.format("Current page title is: %s", _driver.getTitle())); | |
| } | |
| } | |
| public WebElement getElement(By selector) { | |
| try { | |
| return _driver.findElement(selector); | |
| } catch (Exception e) { | |
| System.out.println(String.format("Element %s does not exist - proceeding", selector)); | |
| } | |
| return null; | |
| } | |
| public void sendKeys(By selector, String value) { | |
| WebElement element = getElement(selector); | |
| clearField(element); | |
| try { | |
| element.sendKeys(value); | |
| } catch (Exception e) { | |
| throw new TestException(String.format("Error in sending [%s] to the following element: [%s]", value, selector.toString())); | |
| } | |
| } | |
| public void clearField(WebElement element) { | |
| try { | |
| element.clear(); | |
| } catch (Exception e) { | |
| System.out.print(String.format("The following element could not be cleared: [%s]", element.getText())); | |
| } | |
| } | |
| public void click(By selector) { | |
| WebElement element = getElement(selector); | |
| waitForElementToBeClickable(selector); | |
| try { | |
| element.click(); | |
| } catch (Exception e) { | |
| throw new TestException(String.format("The following element is not clickable: [%s]", selector)); | |
| } | |
| } |
This class not working Null pointer exception throwing error
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
public void sendKeys(By selector, String value) {
WebElement element = getElement(selector);
clearField(element);
try {
element.sendKeys(value);
} catch (Exception e) {
throw new TestException(String.format("Error in sending [%s] to the following element: [%s]", value, selector.toString()));
}
}
if I call this method in other class then what value pass to the by selector ? ex I am trying to pass loginid.commonutils.sendkeys( "id","name")