Thursday, 12 September 2013

Basic Selenium Functions for Newbies

This section is for newbies about selenium webdriver. There will be frequent updates on this.

close()
Closes current active window.
driver.close();

#PYTHON
self.driver.close()

#RUBY
@driver.close



quit()
Quits the driver and closes every windows.
driver.quit();

#PYTHON
self.driver.quit()

#RUBY
@driver.quit



getTitle()
Get the current page title.
driver.getTitle();

#PYTHON
#1 | print page title
print driver.title
#2 | assert text in the page_title
assert "your text" in driver.title
#3 | assert text in the page_title
self.assertIn("your text", driver.title) 
#4 | assert page_title
self.assertEquals("your page_title", driver.title, "Asserting Title")

#RUBY
#1 | print page title
puts @driver.title
#2 | assert page_title
assert_equal('your text', @driver.title)



getPageSource()
Retrieves all the page source.

#1 | print page_source of a web page
System.out.println(driver.getPageSource());
#2 | assert text in a web_page
driver.getPageSource().contains("your text");
#3 |
boolean b = driver.getPageSource().contains("your text");
assertTrue(b); 
#4 |
Boolean b =  driver.getPageSource().contains("your text");
System.out.println(b);

#PYTHON
#1 | print page_source of a web page
print driver.page_source
#2 | assert text in a web_page
assert "your text" in driver.page_source

#RUBY
#1 | print page_source of a web page
puts @driver.page_source
#2 | assert text in a web_page
assert(@driver.page_source.include?("your text"))



findElement()
Finds first element within the current page. 
WebElement element = driver.findElement(By.xpath("//div[@class='google-header-bar']"));



findElements()
Finds all the elements within the current page.

1| Store Length
driver.get("https://accounts.google.com/");
List<WebElement> scriptcount = driver.findElements(By.xpath("//script[@type='text/javascript']"));
System.out.println("Overall textboxes: "+scriptcount.size()); 

2| Click Element
driver.get("http://www.indiabookstore.net");
driver.findElement(By.id("searchBox")).sendKeys("Alche");
List <WebElement> listItems = driver.findElements(By.xpath("/html/body/div[4]/ul/li"));
listItems.get(0).click();

3| Track by TagName
List<WebElement> link = driver.findElement(By.id("Value")).findElements(By.tagName("li"));

No comments:

Post a Comment