Site Loader
Auckland, New Zealand
1. How to Open connectivity with Selenium Server.
Selenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.yoursite.com");
selenium.start();
2. How to open Connectivity with Selenium WebDriver
WebDriver driver = new HtmlUnitDriver();
driver.get("http://www.google.com");
3. How to Open a URL in specified Browser
WebDriver driver = new InternetexplorerDriver();
driver.get("http://www.google.com");
4. What are the Page Element finding ways There is not just one way for finding element in web page (Like Name as shown in above question), but also we can find by means of Tag name, ID, Xpath etc. Here is the example code snippet for finding elements.
private static By by(String element)
{
By retVal = null;
String newval;
if(element.startsWith("//")) {
return By.xpath(element);
}
elseif(element.startsWith("LinkText:"))
{
elements = drivers.findElements(By.tagName("a"));
element = element.replace("LinkText:", "");
return By.linkText(element);
}
elseif(element.startsWith("id:"))
{
newval=element.replace("id:", "");
return By.id(newval);
}
elseif(element.startsWith("Radio:"))
{
List elements = drivers.findElements(By.tagName("label"));
element = element.replace("Radio:", "");
for(WebElement findelement : elements)
{
if(element.contains(findelement.getText()))
{
retVal = By.id(element);
break;
}
}
return retVal;
}
else{
return By.name(element);
}
}
5. Code Snippet to identify following This section will give some code reference to some of the most commonly used control while working with Selenium. Textbox
WebElement txtBox = drivers.findElement(By.name(“UserNameTxt”));
txtBox.SendKey(“This is Test entry”);
Checkbox Check box value can be selected by means of click operation
WebElement ChkBox = drivers.findElement(By.name(“Male”));
ChkBox.Click();
DropDownBox
Select ddl = new Select(findelement(By.name("Option")));
ddl.selectbyvisibletext("Yes");
5.Code to Verify the entered data for following controls This section will give some code reference to the above control for verification of whether the data has correctly entered into elements (Controls of Webpage) or not A.Textbox
public static boolean textDisplayed(final String locator)
{
return drivers.findelement(By.Name(locator)).isDisplayed();
}
B.Checkbox
public static boolean dataSelected(final String locator)
{
return drivers.findelement(By.Name(locator)).isSelected();
}

Post Author: Karthik kk

One Reply to “Selenium Code Snippets”

Leave a Reply

Your email address will not be published. Required fields are marked *