Showing posts with label Selenumn. Show all posts
Showing posts with label Selenumn. Show all posts

Friday, 15 May 2015

Web Driver API-Drag and Drop



Drag and Drop -Webdriver API Action Class

Now sometimes there may be required to automate the some functionality which has drag and drop functionality. Now we have taken the example of program to perform drag and drop operation. In the below example, as we know that the DragAndDrop divs are in a Frame, First we need to switch to the frame before performing the drag and drop operation. And after that you also need to check for the availability of Source Element and Destination Elements in UI.

Syntax for drag and drop is below:-

Actions action = new Actions(driver);
action.dragAndDrop(Sourcelocator, Destinationlocator).build().perform();
You can also make it as like below:

(new Actions(driver)).dragAndDrop(element, target).perform();
https://www.youtube.com/watch?v=TmHILJhJfFo
https://www.youtube.com/watch?v=odxU3L1OB-M

https://www.youtube.com/watch?v=HPx-LWaYzFE
https://www.youtube.com/watch?v=WlQ-xpOACpE
https://www.youtube.com/watch?v=odxU3L1OB-M
https://www.youtube.com/watch?v=TmHILJhJfFo

Monday, 11 May 2015

StaleElementReferenceExceptions (console in eclipse) in Selenium Webdriver API



During running the automation script we got exception is occur when Firefox driver is trying to perform action on the element which is no longer exists or not valid due to page load or whatever reason.
WebElement element = driver.findElement(By.id("samplescript"));
// Before click on  something happened, DOM has changed due to page refresh, or element is removed //and re-added
element.click();
Now at the below example, the element which you are clicking is no longer valid due to some reason.
So it throws up the exception and its hands and gives control to automation tester, who get to know (the test/app author) should know exactly what may or may not happen.
In order to overcome this situation, Need to explicitly wait until the DOM is in a state where we are about to sure that DOM will not change.
ie, using a WebDriverWait syntex to wait for a specific element to exist:
https://www.youtube.com/watch?v=TmHILJhJfFo
https://www.youtube.com/watch?v=odxU3L1OB-M

 
// times out after 10 seconds
WebDriverWait wait = new WebDriverWait(driver, 200);
// While the following loop runing, the DOM has changes -
// page to be refresh state or element is removed and re-added
wait.until(elementIdentified(By.id("element")));       

// now it look like good - let's click the element
driver.findElement(By.id("sample")).click();
private static Function<WebDriver,WebElement> elementIdentified(final By locator) {
    return new Function<WebDriver, WebElement>() {
        @Override
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);
        }
    };
}
We can also use here implicit waits, in which Webdriver API will check for the element to become present until the specified timeout:
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS)

Sunday, 10 May 2015

How to handle JavaScript popup (alerts), confirmation message and prompts?



Mainly JavaScript popups are produced by web application and they can be easily controlled by the web browser.
Webdriver API offer the ability to cope with JavaScript alerts using Alerts API(application programming interface)
// Handle to the open alert, prompt or confirmation
Alert alertinterce = driver.switchTo().alert();
Alert is an interface. Below are the methods
//Click on OK button on confirmation box.
alert.accept();
// Click on cancel button on confirmation box.
.
alert.dismiss()
//Getting the text which is present on Alert window.
alert.getText();
//Passing the text to the prompt popup like text box on alert window
alert.sendkeys();
//Used to authenticate by passing the credentials like (user name and password)
alert.authenticateUsing(Credentials credentials)

Work with Alerts using Selenium Webdriver API:

Below is the html code for alerts, please copy below code and make ahtml file and pass it to the webdriver API.

<html>
<head>
<title>Selenium Misc Alerts Sample document </title>
</head>
<body>
<h2>Alert Box Example for learning</h2>
<fieldset>
<legend>Alert Box</legend><p>Click on the button to display an alert box.</p>
<button onclick="alertFunction()">Click On</button>
<script>
function alertFunction()
{
alert("Here is an example for alert box!");
}
</script>
</fieldset>
</body>
</html>
Below script will demonstrate you working with alerts popup using above html file.
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
 
public class PopupsHandlingExampled {
         WebDriver driver=new FirefoxDriver();
         @Test
         public void ExampleForAlert() throws InterruptedException
         {
         driver.manage().window().maximize();
         driver.get("file:///E:/path/alertsexample.html");
         driver.findElement(By.xpath("//button[@onclick='alertFunction()']")).click();
         Alert alert=driver.switchTo().alert();
         System.out.println(“Here we go”+alert.getText());
         alert.accept();
         }
}

Work with confirmation popups

Below is the sample code for confirmation Popup, please copy and make a html file and pass it to the webdriver API as below.
<html>
<head>
<title>Selenium misc Confirm popup Sample </title>
</head>
<body>
<h2>Confirm Box Example</h2>
<fieldset>
<legend>Confirm Box</legend>
<p>Click the button to display a confirm box.</p>
<button onclick="confirmFunction()">Click Here</button>
<p id="confirmdemo"></p>
<script>
function confirmFunction()
{
var cb;
var c=confirm("Here is the Example for Confirm Box.\n Press any button!");
if (c==true)
  {
  cb="Clicked on OK button";
  }
else
  {
  cb="Clicked on Cancel button";
  }
document.getElementById("confirmdemo").innerHTML=cb;
}
</script>
</fieldset>
</body>
</html>
Below program will understanding to you working on confirmation popup using above html file.
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
 
public class PopupsHandlingexample {
         WebDriver driver=new FirefoxDriver();
         @Test
         public void ExampleForConfirmBoxtest() throws InterruptedException
         {
         driver.manage().window().maximize();
         driver.get("file:///D:/path/confirmation.html");
         driver.findElement(By.xpath("//button[@onclick='confirmFunction()']")).click();
         Alert alert=driver.switchTo().alert();
         System.out.println(alert.getText());
         alert.dismiss();
         }
}

Work with Prompt Popups below is the html filed

In below prompt, Enter the text using webdriver sendkeys("text.. like security code") method
Below is the sample script for prompt popup, please copy and make a html file and pass it to the webdriver API.
<html>
<head>
<title>Selenium misc prompt popup Sample </title>
</head>
<body>
<h2>Prompt Box Example for test purpose</h2>
<fieldset>
<legend>Prompt Box</legend>
<p>Click on the button for understanding the prompt box.</p>
<button onclick="promptFunction()">Click on me</button>
<p id="promptdemo"></p>
<script>
function promptFunction()
{
var x;
var person=prompt("Please enter your login name","Your user name");
if (person!=null)
  {
  x="Hello " + person + "! Welcome to Selenium misc..";
  document.getElementById("promptdemo").innerHTML=x;
  }
}
</script>
</fieldset>
</body>
</html>
Below automation script will demonstrate you working on prompt popup using above html file.
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
 
public class PopupsHandling {
         WebDriver driver=new FirefoxDriver();
         
         @Test
         public void ExampleForPromptBoxfortest() 
         {
         driver.manage().window().maximize();
         driver.get("file:///C:/path/prompt.html");
         driver.findElement(By.xpath("//button[@onclick='promptFunction()']")).click();
         Alert alert=driver.switchTo().alert();
         driver.switchTo().alert().sendKeys("Helllo");
         alert.accept();
         System.out.println(alert.getText());
         }
}