Friday, 9 January 2015

Sample for clickAndHold, dragAndDropBy

Scenario :

Launch  http://w ww.myntra.com/men-deals?f=price%3A399%2C1999.

On the left pane,on the slider  for price range , Click and hold the left button and move it right to an offset of say, 150.


Note:

This can be done using two methods :

1.clickAndHold(webelement).moveByOffset(x, y)

2.dragAndDropBy(webelement,x, y)

In our case the offset 'y' will be 0, as we just want to move the slider horizontally.We set the limit for 'y' when a vertical movement of the element is needed.

Hence, we set the offset for 'x' as 150 - to move horizontally

Let's see how this can be done.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class MoveByOffsetDemo {

public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.get("http://www.myntra.com/men-deals?f=price%3A399%2C1999");

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

WebElement we = driver.findElement(By.className("from"));

Actions act = new Actions(driver);

act.clickAndHold(we).moveByOffset(150, 0).build().perform();

// act.dragAndDropBy(we,150, 0).build().perform();Even this will work

Thread.sleep(5000l);

driver.quit();

}
}


Pay It Forward...

Lots of energy ..

DR







Thursday, 8 January 2015

Sending capital text to textbox using Actions class

Scenario : Launch http://www.flipkart.com . Send the text "watches" in capital letters to the search box on top of the page.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.KeyDownAction;
import org.openqa.selenium.interactions.KeyUpAction;

public class SendCapsText {

public static void main(String[] args) throws InterruptedException {

WebDriver driver = new FirefoxDriver();

driver.navigate().to("http://www.flipkart.com/");

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

WebElement we = driver.findElement(By.name("q"));

Actions act = new Actions(driver);

act.moveToElement(we).click().keyDown(Keys.SHIFT)
.sendKeys("watches") .keyUp(Keys.SHIFT).build().perform();


       Thread.sleep(10000l);

driver.quit();


}

}


Pay It Forward...

Lots of energy ..

DR

Wednesday, 7 January 2015

Mouse hover using Actions class

Scenario : Launch http://www.flipkart.com. Hover the mouse on the tab named 'ELECTRONICS' and select the option 'Lenovo Yoga Tablet 2' from the menu.


import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class MouseHover {

public static void main(String[] args) throws InterruptedException {

WebDriver driver = new FirefoxDriver();

driver.navigate().to("http://www.flipkart.com/");

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

// The element on which to mouse hover
WebElement we = driver.findElement(By
.xpath("//a/span[text()='Electronics']"));

Actions act = new Actions(driver);

act.moveToElement(we).perform();

// The element to be selected from drop down
WebElement we1 = driver
.findElement(By.linkText("Lenovo Yoga Tablet 2"));

we1.click();

// act.moveToElement(we1).click().build().perform();
// Even this will work instead of we1.click()

Thread.sleep(5000L);

driver.quit();

}

}


Pay It Forward...

Lots of energy ..

DR

Monday, 5 January 2015

Handling alerts

An alert box appears in scenarios such as , when we try to submit a form or when we try to delete a record.

For people who may get confused with handling alerts and popups, especially those who are new to webdriver, here's a tip : Alert has only buttons such as OK and Cancel. So that's how we identify an alert.

If we want to click ok, we implement the accept() method of the Alert interface. 

If we want to click cancel in an alert, we implement the dismiss() method of the Alert interface.

In the example below, we'll launch the website and click on the submit button at the bottom of the page. This triggers an alert, that has only one button - 'OK'. We click on it , and then the alert disappears.

public class AlertDemo {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();

driver.navigate().to("https://www.tin-nsdl.com/customerfeedback.php");
driver.manage().window().maximize();

WebDriverWait wait = new WebDriverWait(driver, 20); //WebDriverWait - Interface
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.name("Submit")).click();
wait.until(ExpectedConditions.alertIsPresent());//ExpectedConditions - class
Alert alr = driver.switchTo().alert();//Alert - interface
alr.accept();
driver.quit();
}

}



Note:

In the above piece of code, we have used WebDriver wait, to make sure , the webdriver waits untill the alert appears.

If this is not done and if the driver.switchTo().alert() statement is executed before the alert actually opens , then there will be an exception : NoAlertPresentException



Pay It Forward...

Lots of energy ..

DR