Thursday 2 January 2014

Select methods in selenium Webdriver...

Select Methods
The following are the most common methods used on drop-down elements.

selectByVisibleText() :
deselectByVisibleText():

Selects/deselects the option that displays the text matching the parameter.
Parameter: The exactly displayed text of a particular option

selectByValue()
deselectByValue()

Selects/deselects the option whose “value” attribute matches the specified parameter.
Parameter: value of the “value” attribute remember that not all drop-down options have the same text and “value”, like in the example below.

selectByIndex()
deselectByValue()


Selects/deselects the option at the given index.
Parameter: the index of the option to be selected


isMultiple()

Returns TRUE if the drop-down element allows multiple selections at a time; FALSE if otherwise.
Needs parameters needed

deselectAll ():


Clears all selected entries. This is only valid when the drop-down element supports multiple selections.

No parameters needed

Navigate commands in Selenium Webdriver...


Navigate commands:
 These commands allow you to refresh go-into and switch back and forth between different webpages.

navigate ().to() :        It automatically opens a new browser window and fetches the
                                 page that  you specify inside its parentheses.

                                It does exactly the same thing as the get() method.

navigate().refresh() : It refreshes the current page


navigate().back():       Takes you back by one page on the browser’s history.

navigate().forward():  Takes you forward by one page on the browser’s history.

Tuesday 26 November 2013

How can I read background-color of an element in selenium Webdriver?



driver.get("http://www.google.co.in/");
String color = driver.findElement(By.name("btnK")).getCssValue("background-color");
   
System.out.println("The background color of Google search button"+color);

Wednesday 6 November 2013

How to find child elements in Selenium Web driver




When same object is getting duplicated for N number of parent, then user can refer parent and child relationship to identify the object in browser.

WebElement bodyElement = driver.findElement(By.id("body"));
WebElement bodyElement1 = bodyElement.findElement(By.id("body"));
System.out.println(bodyElement1.findElement(By.id("TotalScore")).getText());

User want to read all child elements of div(parent) container by using following methods.

WebElement bodyElement = driver.findElement(By.id("body"));   
List divElements = bodyElement.findElements(By.className("div"));
for(WebElement childDiv : divElements){
 List fieldSetEle = childDiv.findElements(By.tagName("fieldset"));
 System.out.println(childDiv.getTagName()+":"+fieldSetEle.size());
    }  

How to Identify popup window in Selenium Webdriver....




1. First Check whether its popup window or IFrame window by using IE Developer or Find bug tools.

2. If its popup window then use following code :

driver.findElement(By.xpath("")).click();

Set s=driver.getWindowHandles();

This method will help to handle of opened windows other than parent 



Iterator ite=s.iterator();

while(ite.hasNext())

{

String popupHandle=ite.next().toString();

if(!popupHandle.contains(mwh))

{

driver.switchTo().window(popupHandle);

/**/here you can perform operation in pop-up window**

//After finished your operation in pop-up just select the main window again

driver.switchTo().window(mwh);

}

}

This method will help to filter out of all opened windows


1) String parentWindowHandle = browser.getWindowHandle(); // save the current window handle.

WebDriver popup = null;

Iterator windowIterator = browser.getWindowHandles();

while(windowIterator.hasNext()) { 

String windowHandle = windowIterator.next(); 

popup = browser.switchTo().window(windowHandle);

if (popup.getTitle().equals("Google") {

break;

}

}

2) Uncomment code for the same popup,

//action in popup "Google". 

popup.findElement(By.name("q")).sendKeys("Thoughtworks");

//action in popup "Google". 

popup.findElement(By.name("btnG")).submit();"


3) After the popup actions, switch the driver back to the parent window,


browser.close(); // close the popup.

browser.switchTo().window(parentWindowHandle); // Switch back to parent window.
If its IFrame then use following line.

String parentWindowHandle = driver.getWindowHandle(); 

driver.switchTo().Frame(driver.findelement(by.id("iframeid")));
// perform ur actions.
//revert back to parent control

driver.switchTo().window(parentWindowHandle);

Thursday 1 August 2013

Grid with SeleniumWebdriver


import java.io.File;
 import java.net.MalformedURLException;
 import java.net.URL;

import org.junit.AfterClass;
 import org.openqa.selenium.*;
 import org.openqa.selenium.chrome.ChromeDriver;
 import org.openqa.selenium.ie.InternetExplorerDriver;
 import org.openqa.selenium.remote.DesiredCapabilities;
 import org.openqa.selenium.remote.RemoteWebDriver;
 import org.testng.annotations.*;

public class GridWithWebdriver {

 public WebDriver driver;

 @Parameters({"browser"})
 @BeforeClass
 public void setup(String browser) throws MalformedURLException {

 DesiredCapabilities capability=null;
 //setting Chrome driver location
 System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\lib\\chromedriver.exe");
if(browser.equalsIgnoreCase("firefox")){
 System.out.println("firefox");
 capability= DesiredCapabilities.firefox();
 capability.setBrowserName("firefox");
capability.setPlatform(org.openqa.selenium.Platform.ANY);
 //capability.setVersion("");
 }

 if(browser.equalsIgnoreCase("chrome")){
 System.out.println("chrome");
 capability= DesiredCapabilities.chrome();
 capability.setBrowserName("chrome");
capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
 //capability.setVersion("");
 }

 driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
 driver.manage().window().maximize();
 driver.navigate().to("http://google.com");

 }

 @Test
 public void test_first() throws InterruptedException{
 Thread.sleep(3000);
 WebElement search_editbox = driver.findElement(By.name("q"));
 WebElement search_button = driver.findElement(By.name("btnG"));
 search_editbox.clear();
 search_editbox.sendKeys("first");
 search_button.click();
 driver.navigate().to("http://yahoo.com");
 String MYURL=driver.getCurrentUrl();
 System.out.println(MYURL);
 Thread.sleep(3000);
 }

 @Test
 public void test_second() throws InterruptedException{
 driver.navigate().to("http://CNN.com");
 String MYA=driver.getCurrentUrl();
 System.out.println(MYA);
 Thread.sleep(3000);
 driver.navigate().to("http://google.com");
 WebElement search_editbox=driver.findElement(By.name("q"));
 WebElement search_button=driver.findElement(By.name("btnG"));
 search_editbox.clear();
 search_editbox.sendKeys("second");
 search_button.click();
 }

 @AfterClass
 public void tearDown(){
 driver.close();
 }
 }
 //-Then started the selenium grid as per the below.

java -jar selenium-server-standalone-2.32.0.jar -role hub
 java -jar selenium-server-standalone-2.32.0.jar -role webdriver -hub http://localhost:4444/grid/register -browser browserName=firefox,platform=WINDOWS
 java -jar selenium-server-standalone-2.32.0.jar -role webdriver -hub http://localhost:4444/grid/register -browser browserName=chrome,platform=WINDOWS -port 5556

//My testng.XML file as per the below.

<suite name="Same TestCases on Different Browser" verbose="3" parallel="tests" thread-count="2">
<test name="Run on Mozilla Firefox">
 <parameter name="browser" value="firefox"/>
 <classes>
 <class name="GridWithWebdriver"/>
 </classes>
 <test name="Run on goolge Chrome">
 <parameter name="browser" value="chrome"/>
 <classes>
 <class name="GridWithWebdriver"/>
 </classes>
 </test>
</test>
</suite>


When i ran the XML file as testng suite below error occurred.Hope any one may able to help.



Friday 26 July 2013

Upload Files in Selenium Webdriver

Please go through below code ......


package Sample;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class uploadfile {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.files.com/");
driver.manage().window().maximize();
WebElement uploadElement = driver.findElement(By.xpath(".//*[@id='uploadFields']/input"));
//WebElement uploadElement = driver.findElement(By.id("uploadfile_0"));
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
uploadElement.sendKeys("C:\\Users\\shivap\\Desktop\\24-1374658179-13.jpg");
driver.close();

}
}

Happy Testing