Fundamental knowledge for using Selenium
Type of web elements:
WebElement represents an HTML element. The most common type of web elements are:
- Text box, Text-area
- Button
- Drop Down
- Hyperlink
- Check Box
- Radio Button
Locator in Selenium:
A locator is the HTML properties of a web element which can be used as an address to identifies a web element within the web page. Locator tells Selenium about the web element which Selenium need to perform an action on.
There are many different types of locators in Selenium: ID, ClassName, Name, TagName, LinkText, PartialLinkText, Xpath, CSS Selector, DOM.
What is an Xpath?
XPath is designed to allow the navigation of XML documents. In selenium, Xpath is used to locate a web element based on that web element’s XML path.
XML( Extensible Markup Language) is used to store and transport data. Similarly as HTML tags, XML stores data in a key-value pair, for example, <Element> Element content goes here./ </Element>.
Type of X-Path: XPath can be created in 2 ways
- Relative XPath: is a path which starts from the current location and is prefixed with a Double Slash “//” “. For example: //span[@class=’Email’]
- Absolute XPath: is a path which starts from a root path/node and is prefixed with a Single Slash “/”. For example: /html/body/div/div[@id=’Email’]
Click here to see: How to write your own relative XPath.
Basic commands in Selenium C#:
1. Navigation commands:
- Navigate To Command: go to specific URL. For example:
driver.Navigate().GoToUrl( "http://autoqa.blogtown.co.nz");
- Forward Command : go to forward by one page on the browser’s history. For example:
driver.Navigate().Forward();
- Back Command: back by one page on the browser’s history. For example:
driver.navigate().back();
- Refresh Command: refresh the current page. For example:
driver.navigate().refresh();
2. Launch the browser using WebDriver:
Syntax to launch Browser: WebDriver driver = new FirefoxDriver();
Selenium supports different drivers which are:
- FirefoxDriver
- InternetExplorerDriver
- ChromeDriver
- SafariDriver
- OperaDriver
- AndroidDriver
- IPhoneDriver
- HtmlUnitDriver
3.Find if an element in displayed on the screen:
Using these methods to check the visibility of the web elements: isDisplayed(), isSelected(), isEnabled(). For example:
boolean btnLogin = driver.findElement(By.id(“login”)).isDisplayed(); boolean btnLogin = driver.findElement(By.id(“login”)).isSelected(); boolean btnLogin = driver.findElement(By.id(“login”)).isEnabled();
4. Locate element on the web page:
WebDriver gives us Find Element and Find Elements methods to locate an element on the web page by its locator (ID, ClassName, Name, TagName, LinkText, PartialLinkText, Xpath, CSS Selector)
- Find Element(): returns the first WebElement object which matches to the specified locator value.Otherwise it throws an exception.
IWebElement editButton = driver.FindElement(By.Id("edit"); IWebElement editButton = driver.FindElement(By.Name("editname"); IWebElement editButton = driver.FindElement(By.XPath("//*[@id='container']/div/div/h2");
- Find Elements(): returns all web elements (strore in List) which match to the specified locator value.It can return an empty list if no DOM elements. To find more than one web element in the list we can use a loop to go through the list and get the one we need.
IList<IWebElement> list= driver.FindElements(By.Class("general")); for (int i = 0; i < list.Count; i++) { if (list[i].Text == selectValue) { listValue[i].Click(); break; } }
5. Web element basic commands:
// click on element : button, link driver.findElement(By.linkText("Link")).click(); // clear content of element:textbox, text area. driver.findElement(By.id("UserName")).clear(); // input text driver.findElement(By.id("UserName")).sendKeys("ToolsQA"); // get text driver.findElement(By.id("UserName")).Text;
6. Selecting value of dropdown: using WebDriver’s Select class.
//selectByValue: Select selectByValue = new Select(driver.findElement(By.id(“ID”))); selectByValue.selectByValue(“test”); //selectByVisibleText: Select selectByVisibleText = new Select (driver.findElement(By.id(“ID”))); selectByVisibleText.selectByVisibleText(“test”); //selectByIndex: Select selectByIndex = new Select(driver.findElement(By.id(“ID”))); selectByIndex.selectByIndex(2);
7. Difference betweendriver.close() and driver.quit command?
- close(): closes the web browser window that is being currently accessed by the WebDriver the user is currently working on).
- quit(): closes down all the windows that the program has opened.
8. Waits method in WebDriver:
WebDriverWait is present in the OpenQA.Selenium.Support.UI namespace which can be used to wait for the page or any element of a page is loaded. There are two types of wait used in selenium webdriver.
- Implicit Wait: are used to instruct the browser to wait up to a default waiting time between each consecutive test step/command across the entire test script.
- Explicit wait: are used to instruct the browser to wait until a particular condition is met or the maximum time has elapsed. It is applied for a particular instance only.
9.Handle web based pop up?
There are the four methods that can be used to handle the Alert interface.
// dimiss the Alert Base.driver.SwitchTo().Alert().Dismiss(); // Accept the Alert Base.driver.SwitchTo().Alert().Accept(); // Send value to the Alert Base.driver.SwitchTo().Alert().SendKeys("abc"); // Get text on the Alert Base.driver.SwitchTo().Alert().Text.ToString();
Selenium is an automation testing tool which supports only web application testing. Therefore, windows pop up cannot be handled using Selenium.
10. Assert title of the web page:
// Assert title driver.Title;
11. Mouse hover on a web element using WebDriver:
We use Action Interface to mouse hover on web element
IWebDriver driver = new FirefoxDriver() Actions builder = new Actions(driver) builder.MoveToElement(myelement).Build().Perform();
12. How to handle frame in WebDriver:
- To switch the context to frame : _driver.SwitchTo().Frame(indext)
- To switch back to parent frame : _driver.SwitchTo().ParentFrame()
QA interview questions Selenium Introduction