Showing posts with label WebDriver. Show all posts
Showing posts with label WebDriver. Show all posts

Thursday, April 22, 2021

WebDriver - Simple way to assert List values (Select List box)


Below is one of the simple and straight forward way to assert list of values. 

This is applicable when we want to validate table data or values from multiple webelements.

List<String> expectedData = new ArrayList<>(Arrays.asList("Madan","Blog"));

 List<String> optionValues = new ArrayList<>();

new Select(element).getOptions().stream().forEach(listOption -> optionValues.add(listOption.getText()));

Assert.assertEquals(optionValues,expectedData);

Sunday, April 8, 2012

Selenium usage tips- cookbook for identifying elements in webpage.

Selenium usage tips- cookbook for identifying elements in webpage.
XPath, CSS, DOM and Selenium: The Rosetta Stone
http://www.simple-talk.com/content/article.aspx?article=1269

Wednesday, March 14, 2012

How to get the Element type in Selenium

Below command can be used to get the Element type i.e input,textarea,select..etc
selenium.getEval(String.format("this.browserbot.getCurrentWindow().document.getElementById('%s').type", ElementId)

Wednesday, January 4, 2012

Selenium2 webelement.click is not working in Internet Explorer

There is some issue with few versions of IE (includes OS).
we can use webelement.sendKeys("\n") instead of webelement.click();

Tuesday, December 20, 2011

Sunday, November 13, 2011

Selenium - XPATH - Location Of text...


If we want to click on some text which we dont know the Id,Name
we can use selenium.click("xpath=//*[text()=\"Search\"]")
To be more specific we can use
selenium.click("xpath=//*[@id='ParentDivId']//*[text()='Search']");



Wednesday, November 2, 2011

WebDriver - Listbox and few other details

In latest WebDriver , we have to use sendKeys to select a value from the listbox.
RenderedWebElement class has been depreceated.

Normal Driver instance can be as follows:
  firefox:
            FirefoxProfile firefoxProfile = new FirefoxProfile();
            firefoxProfile.setAcceptUntrustedCertificates(true);
            localDriver = new FirefoxDriver(firefoxProfile);
            localDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            driver = localDriver;
         
 iexplore:
            DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(
     InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
                            true);
            ieCapabilities
                    .setCapability(CapabilityType.HAS_NATIVE_EVENTS, true);
            ieCapabilities.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
            ieCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            ieCapabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT,
                    true);
            localDriver = new InternetExplorerDriver(ieCapabilities);
           driver = localDriver;





Thursday, October 20, 2011

WebDriver waitForElement - waits for an element until given number of secs

public void waitForElement(final By by, long timeout) throws Exception {

(new WebDriverWait(DriverFactory.getDriver(), timeout))
.until(new ExpectedCondition() {
public Boolean apply(WebDriver d) {
return isElementPresent(by);
}
});
}

The above code can be used to wait for an element until it is displayed in webpage.
On new page loads we can use this command to wait before performing a new action by waitForElement which is a mandatory element to be displayed on page/section/frame refresh completes.

Thursday, August 18, 2011

Selenium Window target = "_blank" issue

For example if you have a link like



Upon click mylink , the url loads into a new window ,  to do some operations in newly opened window it is painful to identify the windowName ..etc

And the solution for that is
selenium.open("/test.html");
selenium.getEval("this.page().findElement(\"link=mylink with target _blank\").target='maddys_window'");
selenium..getEval("selenium.browserbot.getCurrentWindow().open('', 'maddys_window')");
selenium.click("link=mylink with target _blank");
Thread.sleep(2000);
selenium.selectWindow("maddys_window");
selenium.windowFocus();
......
......
selenium.close(); //to close maddys_window
selenium.selectWindow("null");  //to get focus to main window
....
...

Wednesday, July 20, 2011

How to do doubleClick on WebElement , WindowMaxmize Using Webdriver

 To maximize the active Window :

public void windowMaximize() {
((JavascriptExecutor) driver).executeScript("if (window.screen){window.moveTo(0, 0);window.resizeTo(window.screen.availWidth,window.screen.availHeight);};");
}

To DoubleClick an Element

public void doubleClick(WebElement webElement) {
//For FF browser.
((JavascriptExecutor)driver).executeScript("var evt = document.createEvent('MouseEvents');"
+ "evt.initMouseEvent('dblclick',true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0,null);"
+ "arguments[0].dispatchEvent(evt);",webElement);

//For IE
//((JavascriptExecutor)driver).executeScript("arguments[0].fireEvent('ondblclick');", webElement);
}

Thursday, March 31, 2011

Get total number of rows from a HtmlTable - Webdriver

Below code can be used to get rowcount from a Html table. If table not exists returns -1.

private int getRowCount(By by) throws Exception {
        try {
            WebElement table = driver.findElement(by);
            List rows = table.findElements(By.tagName("tr"));
            return rows.size();
        } catch (Exception e) {
            return -1;
        }

    }

Friday, March 18, 2011

Clear contents and input a value webdriver

public void inputValue(WebElement txtbox
 ,String value) throws Exception{ 
txtbox
.sendKeys(Keys.chord(Keys.CONTROL, "a"), value);
}

Thursday, February 24, 2011

WebDriver - Handling listboxes

1. Select value from a listbox

public void selectValue(WebElement listbox, String value) {
        if (listbox != null && !value.isEmpty()) {
            List listValues = listbox.findElements(By.tagName("option"));
            for (WebElement option : listValues) {
                if (option.getText().equals(value)) {
                    option.setSelected();
                    break;
                }
            }
        }
    }


2. Get selected value from listbox

public String getSelectedValue(WebElement listbox) throws MyException {
        if (listbox == null) {
            throw new MyException("listboxValues = " + listbox);
        }
        List options = listbox.findElements(By.tagName("option"));
        for (WebElement option : options) {
            if (option.isSelected())
                return option.getText();
        }
        return null;
    }


3. Verfiy for a value in listbox
public boolean verifyListboxValue(WebElement listbox,String value) throws IllegalArgumentException {
        if (listbox == null) {
            throw new IllegalArgumentException("listboxValues = " + listbox);
        }
        List options = listbox.findElements(By.tagName("option"));
        for (WebElement option : options) {
            if (option.getText().equals(value))
                return true;
        }
        return false;
    }


 class MyException extends Exception{
.....
}

Thursday, February 17, 2011

Selenium RC supporting for Https certificate issue

Download latest selenium1.0.x and include the following code while starting selenium server from code.

RemoteControlConfiguration rcc = new RemoteControlConfiguration();
rcc.setTrustAllSSLCertificates(true);
seleniumServer = new SeleniumServer(rcc);
seleniumServer.start();

Wednesday, February 16, 2011

Accepting Untrusted Certificates - Https Webdriver firefox

Code for resolving https issue :
1. Create a firefox new profile
Refer http://kb.mozillazine.org/Creating_a_new_Firefox_profile_on_Windows

Open firefox , open your application and accept the cerificate manually.
Now add below code in your script (assuming the newly created profile name is WebDriver2).

ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("WebDriver2");
profile.setAcceptUntrustedCertificates(false);
WebDriver driver = new FirefoxDriver(profile);


Thursday, December 16, 2010

WebDriver

WebDriver
Uses Selenium’s javascript Selenium-Core. WebDriver does not depend on a javascript core embedded within the browser, therefore it is able to avoid some long-running Selenium limitations.
Sample:
package
com.sample.tests;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.RenderedWebElement;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class LoginPage {
public static void main(String[] args) throws Exception {
// The Firefox driver supports javascript
WebDriver driver = new FirefoxDriver();

// Open Loginpage
driver.get("http://localhost:8220/login.jsp");

// Enter the query string "Cheese"
WebElement userId = driver.findElement(By.id("loginName"));
WebElement password = driver.findElement(By.id("password"));
WebElement submit = driver.findElement(By.id("submitbtn"));

loginName.sendKeys("Maddy");
password.sendKeys("pass");
submit.click();
try{
if(driver.findElement(By.id("LoggedusrNm"))!=null)
Assert.assertEquals("Done");
}catch(NoSuchElementException e){
Assert.fail("No LoggedUser label available");
}
}
}