Agile Testing, Automation, Automation Framework, best practises, Java, Java Class, Java Object, Object Oriented, Selenium, Testing, Web Automation, Web Testing

Part 2 – Resolution of some major-frequently-seen Selenium utilities.

This blog is a continuation of my previous blog . Here I am collating the data according to my own experience. These methods can be added in the Utility class directly to make wide use of different functionalities.


How to verify current page and expected page in selenium?

//Verify URL location of current page vs. expected page, pass in the expected URL when writing the test.
public void verifyLocation(String expectedUrl) throws IOException, InterruptedException {
String actualUrl = getDriver().getCurrentUrl();
assertTrue(actualUrl.contains(expectedUrl));
}

How to verify URL location in selenium?

// Verify URL location of an opened tab vs the expected URL on that opened tab. Pass in the expected URL when writing the test.
public void verifyTabLocation(String a) throws IOException, InterruptedException {
List browserTabs = new ArrayList(getDriver().getWindowHandles());
getDriver().switchTo().window(browserTabs.get(1));
String actualUrl = getDriver().getCurrentUrl();
assertTrue(actualUrl.contains(a));
getDriver().close();
getDriver().switchTo().window(browserTabs.get(0));
}

How to create random names in selenium?

public static String createRandomName(int randomName)
{
return RandomStringUtils.randomAlphanumeric(randomName).toUpperCase();
}

How to verify current page and expected page in selenium?

// Verify URL location of current page vs. expected page, pass in the expected URL when writing the test.
public void verifyLocation(String a) throws IOException, InterruptedException
String actualUrl = getDriver().getCurrentUrl();
assertTrue(actualUrl.contains(a));
}

How to verify URL location in selenium?

// Verify URL location of an opened tab vs the expected URL on that opened tab. //Pass in the expected URL when writing the test.
public void verifyTabLocation(String a) throws IOException, InterruptedException {
List browserTabs = new ArrayList(getDriver().getWindowHandles());
getDriver().switchTo().window(browserTabs.get(1));
String actualUrl = getDriver().getCurrentUrl();
assertTrue(actualUrl.contains(a));
getDriver().close();
getDriver().switchTo().window(browserTabs.get(0));
}

 How to double Click in selenium?

public static void doubleClick(WebElement element) throws InterruptedException, IOException {
Actions action = new Actions(getDriver());
((JavascriptExecutor) getDriver()).executeScript("arguments[0].scrollIntoView();", element);
action.doubleClick(element).build().perform();
}

How to MoveToElement And double click in selenium?

public static void MoveToElementAnddoubleClick(WebElement element) throws InterruptedException, IOException {
// Creates an instance of Actions class, passing current driver instance.
Actions builder = new Actions(getDriver());
// Calls build() and perform() methods directly on Actions class instance
builder.moveToElement(element).doubleClick().build().perform();
}

How to double Click JS in selenium?

public static void doubleClickJS(WebElement element) throws InterruptedException, IOException {
String doubleClickJS = "if(document.createEvent){var evObj = document.createEvent(‘MouseEvents’);evObj.initEvent(‘dblclick’,"
+ "true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject)"
+ "{ arguments[0].fireEvent('ondblclick');};";
JavascriptExecutor js = (JavascriptExecutor) getDriver();
js.executeScript(doubleClickJS, element);
}

How to move To Element in selenium?

public static void moveToElement(WebElement ele) throws InterruptedException, IOException {
Actions action = new Actions(getDriver());
action.moveToElement(ele).build().perform();
}

How to get Current Date in selenium?

public static String getCurrentDate() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now();
Loggers.info(dtf.format(localDate)); // 2016/11/16
return localDate.toString();
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s