WebDrivers Examples:-
Explicit Waits:-
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(new ExpectedCondition(){
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.id("myDynamicElement"));
}});
Implicit Waits:-
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
Taking a Screenshot:-
import java.io.File;
import java.net.URL;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Testing {
public void myTest() throws Exception {
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"),
DesiredCapabilities.firefox());
driver.get("http://www.google.com");
// RemoteWebDriver does not implement the TakesScreenshot class
// if the driver does have the Capabilities to take a screenshot
// then Augmenter will add the TakesScreenshot methods to the instance
WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenshot = ((TakesScreenshot)augmentedDriver).
getScreenshotAs(OutputType.FILE);
}
}
Fire Fox Profilers:-
---------------------
by using the profilers we can get the firefox Extesions and by default its not show the extesions.
ProfileIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("WebDriver");
profile.setPreferences("foo.bar", 23);
WebDriver driver = new FirefoxDriver(profile);
if the profile isn’t already registered with Firefox:
File profileDir = new File("path/to/top/level/of/profile");
FirefoxProfile profile = new FirefoxProfile(profileDir);
profile.addAdditionalPreferences(extraPrefs);
WebDriver driver = new FirefoxDriver(profile)
Enable the native events in firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver driver = new FirefoxDriver(profile);
Locating UI Elements (WebElements):-
----------------------------------
this can be done bye two ways one is by using the webdriver instance, second one is by using the webelement
By ID, By Name,
By Class Name:-
“Class” in this case refers to the attribute on the DOM element.
example:
List cheeses = driver.findElements(By.className("cheese"));
By Tag Name:-
WebElement frame = driver.findElement(By.tagName("iframe"));
By Link Text:-
cheese>
WebElement cheese = driver.findElement(By.linkText("cheese"));
By Partial Link Text:-
search for cheese>
WebElement cheese = driver.findElement(By.partialLinkText("cheese"));
By CSS:-
If a browser does not have native support for css queries, then Sizzle is used. IE 6,7 and FF3.0 currently use Sizzle as the css query engine.
Beware that not all browsers were created equal, some css that might work in one version may not work in another.
WebElement cheese = driver.findElement(By.cssSelector("#food span.dairy.aged"));
By XPATH:-
At a high level, WebDriver uses a browser’s native XPath capabilities wherever possible. On those browsers that don’t have native XPath support, we have provided our own implementation.
internet exporer doesnt have the Native XPath Support for that we will do manually. Tag and Attribute Name must be lower cased.
List inputs = driver.findElements(By.xpath("//input"));
Using JavaScript:-
WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('.cheese')[0]");
Finding all the input elements to the every label on a page:
List labels = driver.findElements(By.tagName("label"));
List inputs = (List) ((JavascriptExecutor)driver).executeScript(
"var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){" +
"inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;", labels);
User Input - Filling In Forms:-
-------------------------------
the state of checkboxes, and you can use “click” to set something like an OPTION tag selected.
WebElement select = driver.findElement(By.tagName("select"));
List allOptions = select.findElements(By.tagName("option"));
for (WebElement option : allOptions) {
System.out.println(String.format("Value is: %s", option.getAttribute("value")));
option.click();
}
WebDriver’s support classes include one called “Select”, which provides useful methods for interacting with these.
Select select = new Select(driver.findElement(By.tagName("select")));
select.deselectAll();
select.selectByVisibleText("Edam");
Once you’ve finished filling out the form, you probably want to submit it. One way to do this would be to find the “submit” button and click it:
driver.findElement(By.id("submit")).click();
or
WebDriver has the convenience method “submit” on every element.
element.submit();
Moving Between Windows and Frames:-
---------------------------------
driver.switchTo().window("windowName");
For unknown Window titles we can use the window handle method
for (String handle : driver.getWindowHandles())
{
driver.switchTo().window(handle);
}
Drag And Drop Commands:-
------------------------------
Webdriver gives us very facinating soultion by Using the Action clause. here the sample code.....
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(someElement)
Explicit Waits:-
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(new ExpectedCondition
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.id("myDynamicElement"));
}});
Implicit Waits:-
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
Taking a Screenshot:-
import java.io.File;
import java.net.URL;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Testing {
public void myTest() throws Exception {
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"),
DesiredCapabilities.firefox());
driver.get("http://www.google.com");
// RemoteWebDriver does not implement the TakesScreenshot class
// if the driver does have the Capabilities to take a screenshot
// then Augmenter will add the TakesScreenshot methods to the instance
WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenshot = ((TakesScreenshot)augmentedDriver).
getScreenshotAs(OutputType.FILE);
}
}
Fire Fox Profilers:-
---------------------
by using the profilers we can get the firefox Extesions and by default its not show the extesions.
ProfileIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("WebDriver");
profile.setPreferences("foo.bar", 23);
WebDriver driver = new FirefoxDriver(profile);
if the profile isn’t already registered with Firefox:
File profileDir = new File("path/to/top/level/of/profile");
FirefoxProfile profile = new FirefoxProfile(profileDir);
profile.addAdditionalPreferences(extraPrefs);
WebDriver driver = new FirefoxDriver(profile)
Enable the native events in firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver driver = new FirefoxDriver(profile);
Locating UI Elements (WebElements):-
----------------------------------
this can be done bye two ways one is by using the webdriver instance, second one is by using the webelement
By ID, By Name,
By Class Name:-
“Class” in this case refers to the attribute on the DOM element.
example:
Cheddar
Gouda
List
By Tag Name:-
WebElement frame = driver.findElement(By.tagName("iframe"));
By Link Text:-
cheese>
WebElement cheese = driver.findElement(By.linkText("cheese"));
By Partial Link Text:-
search for cheese>
WebElement cheese = driver.findElement(By.partialLinkText("cheese"));
By CSS:-
If a browser does not have native support for css queries, then Sizzle is used. IE 6,7 and FF3.0 currently use Sizzle as the css query engine.
Beware that not all browsers were created equal, some css that might work in one version may not work in another.
milkcheese
WebElement cheese = driver.findElement(By.cssSelector("#food span.dairy.aged"));
By XPATH:-
At a high level, WebDriver uses a browser’s native XPath capabilities wherever possible. On those browsers that don’t have native XPath support, we have provided our own implementation.
internet exporer doesnt have the Native XPath Support for that we will do manually. Tag and Attribute Name must be lower cased.
List
Using JavaScript:-
Cheddar
Gouda
WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('.cheese')[0]");
Finding all the input elements to the every label on a page:
List
List
"var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){" +
"inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;", labels);
User Input - Filling In Forms:-
-------------------------------
the state of checkboxes, and you can use “click” to set something like an OPTION tag selected.
WebElement select = driver.findElement(By.tagName("select"));
List
for (WebElement option : allOptions) {
System.out.println(String.format("Value is: %s", option.getAttribute("value")));
option.click();
}
WebDriver’s support classes include one called “Select”, which provides useful methods for interacting with these.
Select select = new Select(driver.findElement(By.tagName("select")));
select.deselectAll();
select.selectByVisibleText("Edam");
Once you’ve finished filling out the form, you probably want to submit it. One way to do this would be to find the “submit” button and click it:
driver.findElement(By.id("submit")).click();
or
WebDriver has the convenience method “submit” on every element.
element.submit();
Moving Between Windows and Frames:-
---------------------------------
driver.switchTo().window("windowName");
For unknown Window titles we can use the window handle method
for (String handle : driver.getWindowHandles())
{
driver.switchTo().window(handle);
}
Drag And Drop Commands:-
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(someElement)
-
.moveToElement(otherElement)
.release(otherElement)
.build();
- dragAndDrop.perform();
- (Or)
i assumed , you will alreday intialize the webdriver:
driver.get("http://html5demos.com/drag")
target = @driver.find_element(:id, "one")
source = @driver.find_element(:id, "bin")
driver.action.drag_and_drop(target, source).perform
Taking the ScrrenShot :-
Here You will see the code for Taking the screen shot when something goes wrong in your application. its working fine with the Chrome and IE but Firefox its opening the new Empty window. if you guys found any soulution for this plz update accordingly.
public static void capturescreen(String Testname,String step,WebDriver driver)throws Exception
{
String path="FloderPath/html folder/Screenshots/"+Testname+"/"+step+".png";
WebDriver augmentedDriver = new Augmenter().augment(driver);
File scrFile=((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(path));
}
No comments:
Post a Comment