0% found this document useful (0 votes)
36 views15 pages

Selenium Notes 1.04

Uploaded by

dereakshay212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views15 pages

Selenium Notes 1.04

Uploaded by

dereakshay212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Selenium Notes

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:

6) Some Imp methods


driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.navigate().refresh();

7) Difference Between get() and navigate():


8) WebElement:
Anything which is present on the webpage is called as webelement.
Ex: text box, link, image, listbox, checkbox etc

9) Methods of WebElement 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']

12) Xpath: Path of an element present in the webpage.


Absolute : Complete path of an element from root of the
webpage(html), Represented by using /
Relative : Path of any element which is present on web page, It is
represented by using //
Syntax:
//tagName[@attribute=’value’]
tagName[text()=’textValue’]
tagName[contains(text(),'textValue')]
tagName[contains(@attributeName,'attributeValue')]
tagName[starts-with (@attributeName,'attributeValue')]
13) getLocation() and getSize():
WebElement un = driver.findElement(By.id("username"));
//To get the size of an element; height & width
Dimension s = un.getSize();
int h = s.getHeight();
int w = s.getWidth();

//To get location of an element; x-axis & y-axis


Point l = un.getLocation();
int x = l.getX();
int y = l.getY();

14) getCssValue():font-size, color, font-weight, font-family,


background
WebElement x =
driver.findElement(By.xpath("//td[@class='build_title']"));
String x1 = x.getCssValue("font-size");

15) Handling multiple elements:


List<String> allLinks = driver.findElements(By.xpath("//a")).getText();
16)

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 table = driver.findElement(By.xpath(“tablepath”))


List<WebElement> tRows = table.findElements(By.tagName("tr"));
for(int i=0;i<tRows.size();i++){
List<WebElement> tData = tRows.findElements(By.tagName("td"));
for(int j=0;j<tData.size();j++){
System.out.println(tData.get(j).getText());
}
}
19) JavascriptExecutor:
JavascriptExecutor j = (JavascriptExecutor) driver;
//To scroll down
j.executeScript("window.scrollBy(0,500);");
//To scroll up
j.executeScript("window.scrollBy(0,-500);");

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);

// Login button highlighted by yellow color and green box


WebElement login = driver.findElement(By.id("login"));
j.executeScript("arguments[0].setAttribute('style','background:
yellow; border: solid 2px green');", login);

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();

WebElement source = driver.findElement(By.xpath("//a[text()='


BANK ']"));
WebElement des =
driver.findElement(By.xpath("(//li[@class='placeholder'])[1]"));
a.dragAndDrop(source, des).perform();

How to perform drag and drop action without using dragAndDrop()?


a.clickAndHold(src).moveToElement(target).release().perform();

21) ALERTS: There are 3 type


o Alert
o Confirmation
o Prompt
Alert a = driver.switchTo().alert();
System.out.println(a.getText());
a.sendKeys(“abcd”)
a.dismiss(); or a.accept();

22) WINDOW HANDLING:


String parentWindowId = driver.getWindowHandle();
System.out.println("Parent Window ID:" + parentWindowId);
driver.findElement(By.id("loginsubmit")).click();
Set<String> allWindowId = driver.getWindowHandles();
List<String> l=new ArrayList(allWindowId);
//By passing index we can switch the desired window
driver.switchTo().window(l.get(1));

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))

Wait wait = new


FluentWait(driver).withTimeout(Duration.ofSeconds(10)).pollingEver
y(Duration.ofMilliseconds(500)) .ignoring(Exception.class);
wait.until(ExpectedConditions.visibilityOf(element))

25) BROKEN LINKS:


If any link is failed load it’s destination page then that link is called as
broken links.

public class JavaDemo {


public static void main(String[] args) throws IOException {
URL url = new URL("http://www.qspiders.com");
HttpURLConnection con = (HttpURLConnection)
url.openConnection();
int code = con.getResponseCode(); //if code is 200, then link is not
broken System.out.println(code); String msg =
con.getResponseMessage(); //if msg is Ok, then link is not broken
System.out.println(msg); }
}
26)Excel Reading Writing

FileInputStream fis = new FileInputStream("filePath");


XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheet("Sheet1");
int lastRowIndex = sheet.getLastRowNum();
// gives index of lastRow index start from 0
int columnCount = sheet.getRow(0).getLastCellNum() ;
// gives count of columns present in row 0

for(int i = 0;i<=lastRowIndex;i++) {

XSSFRow currntRow = sheet.getRow(i);

for(int j=0;j<columnCount;j++) {

String cellValue = currntRow.getCell(j).toString();


System.out.println(cellValue);
XSSFCell cell = currntRow.createCell(3);
cell.setCellValue("Abc");
}
}
FileOutputStream fos = new FileOutputStream("filePath");
wb.write(fos);
wb.close();

You might also like