First steps toward the framework

Create the following packages in src/main/java:

  • org.packt.invokers
  • org.packt.receivers
  • org.packt.command
  • org.packt.client
  • org.packt.testbase

In the org.packt.testbase package, create our TestBase class. This will serve as the base class for all of our classes.

This base class has methods for the following:

  • Sendkeys
  • Sendkeys using JavascriptExecutor
  • Double click
  • Context click 

You can add additional methods to this test base and the changes will percolate throughout the framework since all of our classes will extend the test base. The following code shows the declarations, the various methods for sending different keystrokes, and clicking elements:

public class TestBase {
public static WebDriver driver = null;
protected static Logger log = null;
protected static Utilities util = null;
protected static Actions actions = null;
protected static JavascriptExecutor js = null;
protected static WebDriverWait wdWait = null;
static String reportFileName = null;
protected static Map<String, String> resultMap = new HashMap<String,
String>();
protected static Fillo fillo = null;
protected static Recordset recordSet = null;
protected static Connection connection = null;
protected static ExtentReports extentReport;
protected static ExtentTest extentTest;
private static Object tempElement;
private static String firstXPath = "//*[text()='";
private static String lastXPath = "']";

public TestBase() throws IOException, FilloException {
}

public static void sendKeys(WebElement element, String text) {
try {
wdWait.until(ExpectedConditions.visibilityOf(element));
element.clear();
element.sendKeys(text);
} catch (NoSuchElementException e){
throw new NoSuchElementException();
}
}


public static void sendKeysTAB(WebElement element, Keys text) {
element.sendKeys(text);
}

public static void clickElement(WebElement element) {
wdWait.until(ExpectedConditions.elementToBeClickable(element)).click();
}

The following code shows the various methods for selecting data from drop-downs. Simply append this code to the preceding code:

public static void selectDrpdwnData(WebElement element, String drpdwnData) {
new Select(element).selectByVisibleText(drpdwnData);
}

public static void selectDrpdwnDataByText(WebElement element,
String drpdwnData) {
List<WebElement> optionList = new Select(element).getOptions();
for (WebElement options : optionList) {
if (options.getText().equalsIgnoreCase(drpdwnData)) {
options.click();
}
}
}

public static void selectDrpdwnDataByText2(WebElement element,
String drpdwnData) {
List<WebElement> optionList = new Select(element).getOptions();
for (WebElement options : optionList) {
if (options.getText().equalsIgnoreCase(drpdwnData)) {
options.click();
}
}
}

public static void selectDrpdwnDataByCTRL(WebElement element,
String drpdwnData) {
List<WebElement> optionList = new Select(element).getOptions();
actions = new Actions(driver);
actions.keyDown(Keys.CONTROL);
for (WebElement options : optionList) {
options.click();
}
actions.keyUp(Keys.CONTROL);
}

The following code is used for double click, right-click, and moving the mouse to a particular element:

public static void doubleClickElement(WebDriver driver, WebElement element)
throws InterruptedException {
scrollintoviewJS(element);
System.out.println(element.getText());
Actions action1 = new Actions(driver).doubleClick(element);
action1.perform();
}

public static void rightClickElement(WebDriver driver, WebElement
element) {
actions = new Actions(driver);
actions.contextClick(element).perform();
}

public static void moveToElement(WebDriver driver, WebElement
element) {
actions = new Actions(driver);
actions.moveToElement(element)
actions.perform();
}

The following code is used to set a text box value, enable the textbox, enter a textbox value, click an element, scroll into the view, and create a web element from its text:

public static void setValueJS(String element, String text) {
String jsscript1 = "document.getElementsByName('" + element
+ "')[0].value='" + text + "';";
js.executeScript(jsscript1);
}

public static void enableTextBoxJS(String element) {
String jsscript1 = "document.getElementsByClassName('" + element
+ "')[0].enabled='true';";
System.out.println(jsscript1);
js.executeScript(jsscript1);
}

public static void enterTextBoxValueJS(String element, String value) {
String jsscript1 = "document.getElementsByClassName('" + element
+ "')[0].value='" + value + "';";
System.out.println(jsscript1);
driver.executeScript(jsscript1);
}

public static void clickElementJS(WebElement element) {
String jsscript1 = "arguments[0].click();";
js.executeScript(jsscript1, element);
}

public static void scrollintoviewJS(WebElement element) {
String jsscript1 = "arguments[0].scrollIntoView(false);";
js.executeScript(jsscript1, element);
}

public static List<WebElement> findWebElementList(String textFill)
throws NoSuchElementException {
tempElement = driver.findElements(By.xpath(firstXPath + textFill
+ lastXPath));
return (List<WebElement>) tempElement;
}
}

These are just the methods that are required for now. We will add more methods as and when required.

If the preceding code does not work and fails on ChromeDriver initiation, try updating your version of Guava in the pom.xml file to 22.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.214.32