Selenium and Java

Showing posts with label Web Controls. Show all posts
Showing posts with label Web Controls. Show all posts

Tuesday, 4 August 2015

Selenium - Handling multiple iFrames in Selenium Webdriver

An inline frame is used to embed another document within the current HTML document. It means iframe is actually a webpage within the webpage which have its own DOM for every iframe on the page.

Once the frame is selected or navigated , all subsequent calls on the WebDriver interface are made to that frame. i.e the driver focus will be now on the frame. What ever operations we try to perform on pages will not work and throws element not found as we navigated / switched to Frame.
We can select an iFrame using the below metthods:

1. by index: frame(index):  We can use index of the frame to get focus on the frame. index start with 0. 

   driver.switchTo().frame(0) ;// focus on 0th frame

   Parameters: Index - (zero-based) index
   Returns: driver focused on the given frame (current frame)
   Throws: NoSuchFrameException - If the frame is not found.

2. by name : frame(Name of Frame) : We can use name of the frame instead of index to get frame.

      driver.switchTo().frame(frameName) ;// focus on frame with name frameName

    Parameters: name Or Id - the name of the frame or the id of the frame element.
    Returns: driver focused on the given frame (current frame)
   Throws: NoSuchFrameException - If the frame is not found

3. by Id: frame(Id of the frame): We can also use Id of the frame as below

  driver.switchTo().frame(frameID) ;// focus on frame with ID as frameId

    Parameters: name Or Id - the name of the frame or the id of the frame element.
    Returns: driver focused on the given frame (current frame)
   Throws: NoSuchFrameException - If the frame is not found

4. by locating frame like other element: frame(WebElement frameElement)

  First locate frame with any web element locator and use webelement.

 Webelement el = driver.findelement(by.xpath(".//iframe[@title='ABC']"));
 driver.switchTo().frame(el);

    Parameters: frameElement - The frame element to switch to.
    Returns: driver focused on the given frame (current frame).
    Throws: NoSuchFrameException - If the given element is neither an iframe nor a frame element. And StaleElementReferenceException - If the WebElement has gone stale.

5. switching outside frame : defaultContent()

  driver.switchTo().defaultContent();

6. switch to multiple frame : 

driver.switchTo().frame(ParentFrame).switchTo().frame(ChildFrame);

Tuesday, 14 April 2015

Selenium - Web table in Selenium Webdriver

Web table is a different type of web element and we can not directly get all data in one variable.
To access web table data we need to locate web table using findelement method and then identified all the tr tags using findelements method.
Code for the same as below.

WebDriver driver =  new InternetExplorerDriver();
WebElement table = driver.findElement(By.id("tableid));
List<WebElement> tablerows = table.findElements(By.tagName("tr")); // To get all Rows

int rowcount  =  tablerows.size();  \\To get count of total rows

for (WebElement tablerow : tablerows )
{
tablerowindex = 0;
     List <WebElement> tablecolumns = tablerow.findelements(By.tagName("td"));
     int columncount = tablecolumns.size();
     for (WebElement tablecolumn : tablecolumns)
     {




      }



Tuesday, 24 March 2015

Selenium - findelements method in selenium webdriver

findElements method is used to locate multiple web element with same attribute.like all the element with type button on the page. It returns list of web element. if no element found then a empty list will be return. Like findelement it will not return null or raise any exception if no element found.

Code for play with findelements as below..

WebDriver driver = new InternetExplorerDriver();
List <webElement> elements = driver.findElements(By.className("button"));

for (WebElement element : elements )
   {
      String elmtext = element.getText();  / to get text from web element.
      boolean dis = element.isDisplayed(); //to check if web element is displayed.
    } 




Monday, 23 March 2015

Selenium - Web Element on webpage in Selenium

1. Edit Box:


Enter "Hello" word in edit box...

WebDriver driver = new InternetExplorerDriver();
WebElement element = driver.findElement(By.ID("txtbx"));
element.clear();    \\ Clear text box 
element.sendKeys("Hello");

2. Drop Down :

Select "India" from country drop down.

WebDriver driver = new InternetExplorerDriver();
WebElement element = driver.findElement(By.ID("dcountry"));
new Select(element).selectByVisibleText("India");

Also we can select by index.
new Select(element).selectByIndex(1);
or
 new Select(element).selectByValue("India");

we can also deselect value from drop down using below code.
new Select(element).deselectByValue("India");

3. Radio button:

Select one of the type radio button.
WebDriver driver = new InternetExplorerDriver();
WebElement element = driver.findElement(By.ID("type"));
element.click();
element.isSelected; // To check if radio button is clicked.

in case of multiple radio button and if we want to select one of the radio button then we can use findByElements to locate list of the radio button.

List <WebElement> radiolist = driver.findElements(By.ID("type"));

4. Checkbox :

Select one of the option checkbo.
WebDriver driver = new InternetExplorerDriver();
WebElement element = driver.findElement(By.ID("option"));
element.click();
element.isSelected; // To check if check box is clicked.

in case of multiple checkboxes and if we want to select one of the checkbox then we can use findByElements to locate list of the checkbox.

List <WebElement> radiolist = driver.findElements(By.ID("option"));

5. WebButton:

To click on web Button or submit form details .

WebDriver driver = new InternetExplorerDriver();
WebElement element = driver.findElement(By.ID("submit"));
element.click();
or
element.submit();

element.isEnabled();        //  To check in button is enable or dissable..
element.isDisplayed();    // To check if button is visible ot not..