Logicdriver.get("http://www.google.co.in"); driver.findElement(By.name("q")).sendKeys("Test"); List<WebElement> autoPopulatedList=driver.findElements(By.cssSelector("tr>td>span")); for(WebElement ele:autoPopulatedList) { System.out.println(e.getText()); }
Example
Output of given code for above search word isselenium rc sendkeys
selenium puthon by
selenium
selenium tutorial
selenium ide
selenium webdriver
selenium rc
selenium ide download
selenium grid
selenium documentationPassing parameters to TestCase using testng
- Some times there is a need to send parameters (like browser name, browser version ..etc).
- May be you want to run the same test case with different values for same attribute.
You can achieve above cases by using @parameter annotation in testngBelow is the example for running same test cases in different browsers (firefox, chrome) by passing different parameters (browser name, version, profile)TestNg ClassgetDriverInstance method implimentationimport org.openqa.selenium.WebDriver; import org.testng.annotations.Parameters; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeTest; import org.testng.annotations.AfterTest; public class ExampleTestCase { private static WebDriver driver; @Parameters({"browser,version"}) @BeforeClass public void beforeClass(String browser,String version,String profile) { driver=getDriverInstance(browser,version,profile); } @BeforeTest public void beforeTest() { } @Test public void f() { //your test code here } @AfterTest public void afterTest() { } @AfterClass public void afterClass() { driver.quit(); } }public static WebDriver getDriverInstance(String browser,String version,String profile) { WebDriver driver=null; if(browser.equals("firefox")) { DesiredCapabilities capability = DesiredCapabilities.firefox(); capability.setVersion(version); capability.setCapability(FirefoxDriver.PROFILE, profile); driver = new FirefoxDriver(capability); } else if(browser.equals("chrome")) { DesiredCapabilities capability = DesiredCapabilities.chrome(); capability.setVersion(version); driver = new FirefoxDriver(capability); } return driver; }
TestNg Suite<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite thread-count="2" name=MyTestSuite" parallel="tests"> <test name="RunInFirefox" preserve-order="false"> <parameter name="browser" value="firefox"><parameter name="version" value="8"/> <parameter name="profile" value="default"> <classes preserve-order="true"> <class name="com.test.TestCase1"/> <class name="com.test.TestCase2"/> <class name="com.test.TestCase3"/> </classes> </test> <test name="RunInChrome" preserve-order="false"> <parameter name="browser" value="chrome"> <parameter name="version" value="21"/> <classes preserve-order="true"> <class name="com.test.TestCase1"/> <class name="com.test.TestCase2"/> <class name="com.test.TestCase3"/> </classes> </test> </suite>
Uploading photo in facebook
1: driver.get("http://www.facebook.com"); 2: driver.findElement(By.id("email")).clear(); 3: driver.findElement(By.id("email")).sendKeys("*******@gmail.com"); 4: driver.findElement(By.id("pass")).clear(); 5: driver.findElement(By.id("pass")).sendKeys("*******"); 6: driver.findElement(By.cssSelector("#loginbutton > input")).click(); 7: driver.findElement(By.linkText("Facebook")).click(); 8: driver.findElement(By.linkText("Add Photos/Video")).click(); 9: driver.findElement(By.xpath("//div[text()='Upload Photos/Video']/following-sibling::div/input")).sendKeys("C:\\form1.csv");
Web application automation is depends on so many factors. Browser, network speed ...etc. We should write unquie code for running in all environments. For achieveing that we need to wait for WebElements before performing any operation on that.
Here we will see some built-in waits in WebDriver.implicitlyWait
Implicit waits are basically your way of telling WebDriver the latency that you want to see if specified web element is not present that WebDriver looking for. (Click here for more information). This will be useful when certain elements on the webpage will not be available immediately and needs some time to load.1WebDriver driver =newFirefoxDriver();2driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);3driver.get("http://www.google.co.in");4WebElement myElement = driver.findElement(By.id("someId"));pageLoadTimeout
Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.1driver.manage().timeouts().pageLoadTimeout(30, SECONDS);setScriptTimeout
1driver.manage().timeouts().setScriptTimeout(30,SECONDS);
Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error.
- While automation web applications in some places we want/need to check particular text is present or not.
In WebDriver (Selenium 2), there is no predefined method for checking this. (In Selenium-RC isTextPresent("text") built-in method exist ) So we need to implement our own method to achieve this.
Let us consider you are searching for text "WebDriver"
boolean isTextPrest=false;
Method-1isTextPrest=driver.findElement(By.tagName("body")).getText().contains("WebDriver");
Method-2isTextPrest=driver.findElement(By.xpath("//*[contains(.,'WebDriver')]")).isDisplayed();
Method-3Selenium sel=new WebDriverBackedSelenium(driver, ""); isTextPresent=sel.isTextPresent("WebDriver");
FYI : Method-1 will take more time than remaining (comparatively)
Firebug command line allows executing JavaScript expressions within context of the current page. Firebug also provides a set of built in APIs and one of them is
Lets consider below iframe
<iframe id="testID" name="testName" src="iframe.html" />
cd().Lets consider below iframe
<iframe id="testID" name="testName" src="iframe.html" />
cd(document.getElementById("testID").contentWindow);switch to a frame by IDcd(window.frames[0]);switch to the first frame in the list of framescd(window.frames["testName"]);switch to a frame using by namecd(window.top);switching back to the top level window
isElementPresent? Method another way
“we can use the findElements() call, and then we just need to check that the size of the list returned is 0.
You can write logic like this.List
elements = driver.findElements(By.Id("element id")); if(elements.size()==0) No element found else Element found - Do you want to take screenshots while automation running?
Using below method you can take screen shots while running the automation. For using below method you need to import below packages.import org.apache.commons.io.FileUtils;
import org.openqa.selenium.TakesScreenshot;
public static void takeScreenShot(WebDriver driver,String fileName) { try { File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile, new File("C:\\screenShot1.png")); } catch (Exception e) { e.printStackTrace(); } }
- Implicit wait
WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Implicit waits are basically your way of telling WebDriver the latency that you want to see if specified web element is not present that WebDriver looking for. So in this case, you are telling WebDriver that it should wait 10 seconds in cases of specified element not available on the UI (DOM).- Implicit wait time is applied to all elements in your script
Explicit wait (By Using FluentWait)public static WebElement explicitWait(WebDriver driver,By by) { Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(20, TimeUnit.SECONDS) .pollingEvery(2, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); WebElement element= wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(By.id("foo")); } }); return element; }
Explicit waits are intelligent waits that are confined to a particular web element. Using explicit waits you are basically telling WebDriver at the max it is to wait for X units of time before it gives up.- Explicit wait time is applied only for particular specified element.
- In Explicit you can configure, how frequently (instead of 2 seconds) you want to check condition
Explicit waitpublic static WebElement explicitWait(WebDriver driver,By by) { WebDriverWait wait = new WebDriverWait(driver, 30); wait.until(ExpectedConditions.presenceOfElementLocated(by)); }
In above method it will wait until either ExpectedConditions become true or Timeout (30 sec).
No comments:
Post a Comment