Selenium Notes 1.04
Selenium Notes 1.04
1) Automation testing:
Testing an application by using any automation tools is called as
automation testing
2 ) Selenium:
Selenium IDE,Selenium Grid ,Selenium-WebDriver,Selenium RC
It’s a free and open source automation tool which is used to
automate any web based applications.
3) High Level Architecture of selenium:
Client/languageBinding WebDriverAPI Driver exe files Browsers
SearchContext =>SupermostInterface
JavascriptExecutor, WebDriver, TakesScreenshot =>Interfaces
RemoteWebDriver => SupermostClass
4) Browser Launching
System.setProperty("webdriver.gecko.driver",
"C:/Users/siva/workspace/Selenium/driver/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
System.setProperty("webdriver.chrome.driver",
"C:/Users/siva/workspace/Selenium/driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
System.setProperty("webdriver.ie.driver",
"C:/Users/siva/workspace/Selenium/driver/IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
System.setProperty("webdriver.opera.driver","C:/Users/siva/worksp
ace/Selenium/ driver/operadriver.exe" ); WebDriver driver = new
OperaDriver();
5) Methods of WebDriver Interface:
10) LOCATORS:
Static methods which are used to identify the elements which are
present the webpage.
All these locators are present in a class called By which is an
Abstract class.
There are 8 types of locators and all the locators takes argument of
type string. They are,
1. Id(String) 2. name(String) 3. className(String) 4. tagName(String)
5. linkText(String) 6. partialLinkText(String) 7. cssSelector(String) 8.
xpath(String)
11) cssSelector
Syntax:
tagnName[attributeName=’attributeValue’]
ex: input[type='password']
17)Drop Down
If the list box is developed by using select tag then we can handle it
by using Select class
WebElement w = driver.findElement(By.name("coffee"));
Select s=new Select(w);
List o = s.getOptions();
for (WebElement x:o) { System.out.println(x.getAttribute("value")); }
WebElement w = driver.findElement(By.name("coffee"));
Select s=new Select(w);
s.selectByVisibleText("With cream & sugar");
18)Table Printing
WebElement table = driver.findElement(By.xpath(“tablepath”))
List<WebElement> tRows = table.findElements(By.tagName("tr"));
for(WebElement rows:tRows){
List<WebElement> tData = rows.findElements(By.tagName("td"));
for(WebElement data:tData){
System.out.println(data.getText());
}
}
WebElement w = driver.findElement(By.xpath("//*[text()='Live
Demo']"));
//scroll down up to particular path
j.executeScript("arguments[0].scrollIntoView(true);", w);
j.executeScript("arguments[0].Click();", w);
20)Screenshot
TakesScreenshot tk=(TakesScreenshot) driver;
File source= tk.getScreenshotAs(OutputType.FILE);
File des=new File("F:/facebook.png");
FileUtils.copyFile(source,des );
21)Actions
WebElement courses = driver.findElement(By.linkText("COURSES"));
Actions a=new Actions(driver);
a.moveToElement(courses).perform();
23) IFRAME:
IFrame is a web page which is embedded in another web page or an
HTML document embedded inside another HTML document.
Ways to Switch IFrame:
1. Switch to frame by index
driver.switchTo().frame(0);
2. Switch to frame by id or name
driver.switchTo().frame("IF1");
3. Switch to frame by webelement
WebElement web = driver.findElement(By.id("IF2"));
driver.switchTo().frame(web);
24) WAITS:
Synchronization:
Matching speed of selenium with the speed of application is called
as synchronization.
we can handle synchronization by using,
o implicit wait o explicit wait o Thread.sleep() o Fluent wait
Implicit wait:
It will handle the synchronization of findElement() and
findElements().Wait for all elements if found immediately proceeds
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
or
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
Explicit wait:
It is used to handle synchronization of any methods including
findElement() and findElements().Here the default time unit is
seconds.
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOf(element));
or
WebDriverWait wait = new WebDriverWait(driver,
Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOf(element))
The following are the Expected Conditions that can be used in
Explicit Wait
1. alertIsPresent()
2. elementSelectionStateToBe()
3. elementToBeClickable()
4. elementToBeSelected()
5. frameToBeAvaliableAndSwitchToIt()
6. invisibilityOfTheElementLocated()
7. invisibilityOfElementWithText()
8. presenceOfAllElementsLocatedBy()
9. presenceOfElementLocated()
10. textToBePresentInElement()
11. textToBePresentInElementLocated()
12. textToBePresentInElementValue()
13. titleIs() 14. titleContains()
15. visibilityOf()
16. visibilityOfAllElements()
17. visibilityOfAllElementsLocatedBy()
18. visibilityOfElementLocated()
Fluent wait:
The fluent wait is used to tell the web driver to wait for a condition,
as well as the frequency with which we want to check the condition
before throwing an "Exception" exception.
Wait wait = new FluentWait(driver).withTimeout(10, TimeUnit.
SECONDS).pollingEvery(500, TimeUnit. MILLISECONDS)
.ignoring(Exception.class);
wait.until(ExpectedConditions.visibilityOf(element))
for(int i = 0;i<=lastRowIndex;i++) {
for(int j=0;j<columnCount;j++) {