TESTEVERYTHING
Thursday, 1 December 2016
Useful Tips in Mobile Automation
Wednesday, 16 November 2016
Actual differences between Explicit and Implicit Waits
Hi All,
As i have already mentioned the differences Explicit and Implicit Waits in my earlier post but now we will see the actual results. there will be two conditions we can think of it what will happen if implicit wait is greater than explicit wait OR implicit wait is lower than explicit wait
lets see what will happen
As i have already mentioned the differences Explicit and Implicit Waits in my earlier post but now we will see the actual results. there will be two conditions we can think of it what will happen if implicit wait is greater than explicit wait OR implicit wait is lower than explicit wait
lets see what will happen
Condition
1: implicit wait is greater than explicit wait
implicitlyWait = 10 seconds
explicitWait
= 2 seconds
///////////////////////////////////////
public
static void main(String[]args) {
FirefoxDriver
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,
TimeUnit.SECONDS);
driver.get("https://www.google.com");
try
{
Date
date = new Date();
System.out.println("
before " + new Timestamp(date.getTime()));
WebDriverWait
wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[contains(text(),'NOT
FOUND ELEMENT')]")));
}
catch (Exception e) {
//
TODO Auto-generated catch block
Date
date = new Date();
System.out.println("
after " + new Timestamp(date.getTime()));
e.printStackTrace();
}
/////////////////////////////////
let see the output in console: taking 10 seconds
/////////////////////////////////
let see the output in console: taking 10 seconds
before 2016-11-16 16:24:33.128
after 2016-11-16 16:24:43.321
Condition
2: implicit wait is lower than explicit wait
implicitlyWait = 2 seconds
explicitWait
= 10 seconds
///////////////////////////////////////
public
static void main(String[]args) {
FirefoxDriver
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2,
TimeUnit.SECONDS);
driver.get("https://www.google.com");
try
{
Date
date = new Date();
System.out.println("
before " + new Timestamp(date.getTime()));
WebDriverWait
wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[contains(text(),'NOT
FOUND ELEMENT')]")));
}
catch (Exception e) {
//
TODO Auto-generated catch block
Date
date = new Date();
System.out.println("
after " + new Timestamp(date.getTime()));
e.printStackTrace();
}
/////////////////////////////////
let see the output in console: taking 10 seconds
/////////////////////////////////
let see the output in console: taking 10 seconds
before 2016-11-16 16:26:53.355
after
2016-11-16 16:27:03.53Monday, 14 November 2016
WebDriver | Automation | launch/Execute on different version of Firefox
Run tests on specific Firefox version | Selenium
Firefox binary
Imagine a situation where you have to test your web application against two different versions of the Firefox browser. By default, when you instantiate FirefoxDriver, the Firefox version that is available on the PATH variable is launched. But if you want to launch a different version of Firefox, we need to use Firefox Binary. For this first steps you have to install all the versions of Firefox which you want to test on your system.
Make sure the installed Firefox versions on different path locations using Custom installation
Installing multiple versions of Firefox
Suppose you have already installed the Firefox(version 22) means default Firefox is already installed. this can be found on your program file location like this..
"C:\Program Files (x86)\Mozilla Firefox"
Now we have to install another Firefox version (lets take Firefox version 23).
1. Download this version from Mozilla site and start installing it.
2. When you reach the following screen in your installation, select Custom.
3. Enter the path as "C:\Mozilla Firefox\" in the path field as shown as follows, and proceed with the installation:
4. Now try to launch Firefox from your code; it will launch Firefox 22 version (default Firefox browser) as it is available in the PATH variable.
5. So, in order to use Firefox 23.0, try to use Firefox Binary. The following is the code example for it:
Here we have to define the path of my another installed Firefox version
import java.io.File;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
private WebDriver driver;
FirefoxBinary binary = new FirefoxBinary(new File("C:\\Mozilla Firefox\\firefox.exe"));
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
private WebDriver driver;
FirefoxBinary binary = new FirefoxBinary(new File("C:\\Mozilla Firefox\\firefox.exe"));
FirefoxProfile profile = new FirefoxProfile();
driver = new FirefoxDriver(binary, profile);
driver.get("http://www.google.com");
Sunday, 13 November 2016
WebDriver Event Listeners
WebDriver Event Listeners
Selenium WebDriver provides a very good framework for tracking the various events that happen while you're executing your test scripts using WebDriver. Many navigation events that get fi red before and after an event occurs (such as before and after navigating to a URL, before and after browser back-navigation, and so on) can be tracked and captured
How it works?
The Listeners use to listen for the events in the registered webdriver. It can be any type of event or action. Say, page navigation, element click, changing values in a text field (analogy to javascript onXYZ () series methods) and even exceptions.
To enable this feature,
- Create our own user defined Event listener class
- Create an EventFiringWebDriverobject by means of webdriver instance,
- And register the Listener to the EventFiringWebDriver instance.
How to create your own event listener classes:
You can create a event listener class in two ways, those are
- By implementing WebDriverEventListener interface
- By extending AbstractWebDriverEventListener class
The following fl ow diagram explains what has to be done to capture all of the events
raised by EventFiringWebDriver during the execution of test cases:
like
// Creating webdriver instance
WebDriver driver = new FirefoxDriver();
// Creating EventFiringWebDriver instance
EventFiringWebDriver eventFiringWD= new EventFiringWebDriver(driver);
// Creating instance of eventListener, that we just defined
EventListenerCreated EventListenerobj =new EventListenerCreated ();
// Register the Listener with the event firing driver
eventFiringWD.register(EventListenerobj );
eventFiringWD.get("https://www.google.co.in/");
eventFiringWD.findElement(By.className("nosuchclassName"));
WebDriver driver = new FirefoxDriver();
// Creating EventFiringWebDriver instance
EventFiringWebDriver eventFiringWD= new EventFiringWebDriver(driver);
// Creating instance of eventListener, that we just defined
EventListenerCreated EventListenerobj =new EventListenerCreated ();
// Register the Listener with the event firing driver
eventFiringWD.register(EventListenerobj );
eventFiringWD.get("https://www.google.co.in/");
eventFiringWD.findElement(By.className("nosuchclassName"));
Step 1- Create a new Class that will implement WebDriverEventListener methods
package testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
public class ActivityCapture implements WebDriverEventListener {
@Override
public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {
}
@Override
public void afterClickOn(WebElement arg0, WebDriver arg1) {
System.out.println("After click "+arg0.toString());
}
@Override
public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
System.out.println("After FindBy "+arg0.toString());
}
@Override
public void afterNavigateBack(WebDriver arg0) {
System.out.println("After navigating back "+arg0.toString());
}
@Override
public void afterNavigateForward(WebDriver arg0) {
System.out.println("After navigating forword "+arg0.toString());
}
@Override
public void afterNavigateTo(String arg0, WebDriver arg1) {
System.out.println("After navigating "+arg0.toString());
System.out.println("After navigating "+arg1.toString());
}
@Override
public void afterScript(String arg0, WebDriver arg1) {
}
@Override
public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) {
}
@Override
public void beforeClickOn(WebElement arg0, WebDriver arg1) {
System.out.println("before click "+arg0.toString());
}
@Override
public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
System.out.println("before FindBY "+arg0.toString());
}
@Override
public void beforeNavigateBack(WebDriver arg0) {
System.out.println("Before navigating back "+arg0.toString());
}
@Override
public void beforeNavigateForward(WebDriver arg0) {
System.out.println("Before navigating Forword "+arg0.toString());
}
@Override
public void beforeNavigateTo(String arg0, WebDriver arg1) {
System.out.println("Before navigating "+arg0.toString());
System.out.println("Before navigating "+arg1.toString());
}
@Override
public void beforeScript(String arg0, WebDriver arg1) {
}
@Override
public void onException(Throwable arg0, WebDriver arg1) {
System.out.println("Testcase done"+arg0.toString());
System.out.println("Testcase done"+arg1.toString());
}
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
package testcases;
import testcases.ActivityCapture;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
public class ListnerDemo {
public static void main(String []args){
System.out.println("Started");
WebDriver driver=new FirefoxDriver();
EventFiringWebDriver event1=new EventFiringWebDriver(driver);
ActivityCapture handle=new ActivityCapture();
event1.register(handle);
event1.navigate().to("http://www.facebook.com");
event1.findElement(By.id("email")).sendKeys("asdsadsa");
event1.findElement(By.id("loginbutton")).click();
event1.quit();
System.out.println("End");
}
}
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
public class ActivityCapture implements WebDriverEventListener {
@Override
public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {
}
@Override
public void afterClickOn(WebElement arg0, WebDriver arg1) {
System.out.println("After click "+arg0.toString());
}
@Override
public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
System.out.println("After FindBy "+arg0.toString());
}
@Override
public void afterNavigateBack(WebDriver arg0) {
System.out.println("After navigating back "+arg0.toString());
}
@Override
public void afterNavigateForward(WebDriver arg0) {
System.out.println("After navigating forword "+arg0.toString());
}
@Override
public void afterNavigateTo(String arg0, WebDriver arg1) {
System.out.println("After navigating "+arg0.toString());
System.out.println("After navigating "+arg1.toString());
}
@Override
public void afterScript(String arg0, WebDriver arg1) {
}
@Override
public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) {
}
@Override
public void beforeClickOn(WebElement arg0, WebDriver arg1) {
System.out.println("before click "+arg0.toString());
}
@Override
public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
System.out.println("before FindBY "+arg0.toString());
}
@Override
public void beforeNavigateBack(WebDriver arg0) {
System.out.println("Before navigating back "+arg0.toString());
}
@Override
public void beforeNavigateForward(WebDriver arg0) {
System.out.println("Before navigating Forword "+arg0.toString());
}
@Override
public void beforeNavigateTo(String arg0, WebDriver arg1) {
System.out.println("Before navigating "+arg0.toString());
System.out.println("Before navigating "+arg1.toString());
}
@Override
public void beforeScript(String arg0, WebDriver arg1) {
}
@Override
public void onException(Throwable arg0, WebDriver arg1) {
System.out.println("Testcase done"+arg0.toString());
System.out.println("Testcase done"+arg1.toString());
}
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
package testcases;
import testcases.ActivityCapture;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
public class ListnerDemo {
public static void main(String []args){
System.out.println("Started");
WebDriver driver=new FirefoxDriver();
EventFiringWebDriver event1=new EventFiringWebDriver(driver);
ActivityCapture handle=new ActivityCapture();
event1.register(handle);
event1.navigate().to("http://www.facebook.com");
event1.findElement(By.id("email")).sendKeys("asdsadsa");
event1.findElement(By.id("loginbutton")).click();
event1.quit();
System.out.println("End");
}
}
Thursday, 10 November 2016
Webdriver/Selenium Difference between methods .isDisplayed() and .isEnabled()?
Hi All,
Method
Method
Last name: <input type="text"
name="lname" enabled><br>
I have seen so many posts where people asked the differences
between .isDisplayed() and .isEnabled() in Selenium/Webdriver automation and
found mostly people are still confused.
The methods .isDisplayed() and .isEnabled() have
nothing in common.
Here I am trying to explain the read differences between .isDisplayed()
and .isEnabled(). As name itself clearly says .isDisplayed() means is my object
are visible or not on my webpage means Am I able to see my element through my
eyes.
Method .
isDisplayed()
:
An
element is considered displayed when it is perceptually visible to the human
eye.
The element displayed algorithm is a
boolean state where true signifies that the element is displayed and false
signifies that the element is not displayed.
To compute the state on element:
- · If the attribute hidden is set, return false.
- · If the computed value of the display style property is "none", return false.
Like
<input type="hidden" name="abc" value="10" id="hiddenFiled1" />
Observe the output, since
element with id hiddenField1 is hidden from web page, so isDisplayed method
return false, whereas isEnabled() method return true.
OR
Refer below link
Try to run with display: block; and display: none;
Method .
isEnabled()
:
An element is considered enabled if it's not
a form control (button, input, textarea, select or option) or when the
user interactions and focus are not blocked with the disabled attribute/property.
Is Element Enabled determines if the referenced element is enabled or not. This operation only makes sense on
form controls.
Like
Last name: <input type="text"
name="lname" disabled><br>
Refer below link
Last name: <input type="text"
name="lname" disabled><br>
Subscribe to:
Posts (Atom)