Quick Tutorial On Selenium For Impatient Learners
What is a selenium ?
If the webpage has more elements with same name, use the method findElements() instead of findElement(). The findElements() method returns a list of all the elements with same name or id or tagname.
List<WebElement> tablesList = driver.findElements(By.tagName("table"));
How to fill the Form using Selenium ?
Use the sendKeys() method to fill a text field , or text area. In order to do that, you need to find the element first using above mentioned guidelines. Here is the sample code to fill the text field.
WebElement textFieldWe = driver.findElement(By.name("First Name"));
textFieldWe.sendKeys("speaking cs");
or
you can combine both two lines like this,
driver.findElement(By.name("FirstName")).sendKeys("speaking cs"));
How to click an Element such as Link, Button Using Selenium ?
Use the click() method of WebElement. Follow the same rules for finding WebElement in the page.
WebElement buttonWe = driver.findElement(By.name("Publish"));
buttonWe.click();
or
combine the above lines like below
driver.findElement(By.name("Publish")).click();
How to submit the Form using Selenium ?
clicking the submit button in the forms won't push your form's data to server. You need to use submit() method of WebElement.
WebElement submitButtonWe = driver.findElement(By.name("submit"));
submitButtonWe.submit();
or
driver.findElement(By.name("submit")).submit();
How to retrieve Attribute from the Element ?
if an Element is associated with Attributes and if you want to get that attribute use getAttribute() method of WebElement.
Eg: <a href = "www.speakingcs.com">speakingcs</a>
first find the anchor tag
WebElement anchorWe = driver.findElement(By.tagName("a"));
System.out.println("url is " + anchorWe.getAttribute("href"));
Retrieving the text associated with the WebElement :
use getText() method to retrieve text of an element. The code snippet is below.
WebElement anchorWe = driver.findElement(By.tagName("a"));
anchorWe.getText(); // gives result as "speakingcs".
How to find InnerHtml and OuterHtml of an Element ?
use getAttribute() method and pass "innerHTML" as argument to get InnterHtml of the element and pass "outerHTML" as argument to get outerHtml. Here is the code.
WebElement anchorWe = driver.findElement(By.tagName("a"));
anchorWe.getAttribute("innerHTML"); // result is <a href = "www.speakingcs.com">speakingcs</a>
anchorWe.getAttribute("outerHTML"); // result will be html code of it's parent tag.
- Selenium is a open-source testing tool, which can interact with the web content inside the real browser such as firefox, google chrome, internet explorer.
- Selenium is used for automating web application & in Regression testing. Regression testing means, testing the application after the code fixes, & enhancements, to make sure that existing features were not broken. This saves a lot of time and manual rework.
- Writing Selenium scripts requires some prior knowledge on java programming.
Let's come to the point,
How selenium interacts with the content inside a browser ?
The answer lies in Webdriver. So what is Webdriver ? Webdriver is a tool in selenium, which can open the real browser and can communicate with it. In java terms, Webdriver is an interface.
How to instantiate Webdriver ?
You can instantiate Webdriver with FirefoxDriver, ChromeDriver, InternetExplorerDriver. The below examples shows how to do.
WebDriver driver = new FirefoxDriver();
WebDriver driver = new ChromeDriver();
WebDriver driver = new InternetExplorerDriver();
Once, after creation of WebDriver object, the Selenium opens appropriate web browser.
How to Open a already developed web application or any URL?
Use get() method of WebDriver to load the Web application or any web page.
driver.get("https://www.gmail.com");
Note: Always mention the protocol (http/ https). otherwise selenium throws an Exception.
How to find an Element existing in the page using Selenium ?
Use WebElement. Any element in the page, such as , Button, Text Field, Link, or any tag will be considered as WebElement , you can find the WebElement, using number of ways,
Finding Element using it's Id :
for example, if you have a tag with id, like <a id = "anchor"> www.speakingcs.com</a> , the code to get this element is.
WebElement anchorWe = driver.findElement(By.id("anchor"));
Finding Element using it's name :
WebElement anchorWe = driver.findElement(By.name("$name of element"));
Finding Element using it's Xpath :
WebElement anchorWe = driver.findElement(By.xpath("$xpath of element"); Here is the sample tutorial from youtube to find xpath.
WebElement anchorWe = driver.findElement(By.name("$name of element"));
Finding Element using it's Xpath :
WebElement anchorWe = driver.findElement(By.xpath("$xpath of element"); Here is the sample tutorial from youtube to find xpath.
Finding Element using it's Class name :
WebElement anchorWe = driver.findElement(By.className("$class of element"));
you can also find element using its css selector,
Finding Element using it's Css selector :
WebElement anchorWe = driver.findElement(By.cssSelector("$css selector of element"));
Finding Element using it's Tag name :
In the webpage , if you have an element like this "<table> <tr> <td> selenium</td></tr></table>" and if it is not associated with name, id, class name, css selector, then you can find the element using it's tag name or xpath.
WebElement anchorWe = driver.findElement(By.tagName("table"));
Finding Multiple Elements with same name, id , or class name :
WebElement anchorWe = driver.findElement(By.className("$class of element"));
you can also find element using its css selector,
Finding Element using it's Css selector :
WebElement anchorWe = driver.findElement(By.cssSelector("$css selector of element"));
Finding Element using it's Tag name :
In the webpage , if you have an element like this "<table> <tr> <td> selenium</td></tr></table>" and if it is not associated with name, id, class name, css selector, then you can find the element using it's tag name or xpath.
WebElement anchorWe = driver.findElement(By.tagName("table"));
Finding Multiple Elements with same name, id , or class name :
If the webpage has more elements with same name, use the method findElements() instead of findElement(). The findElements() method returns a list of all the elements with same name or id or tagname.
List<WebElement> tablesList = driver.findElements(By.tagName("table"));
How to fill the Form using Selenium ?
Use the sendKeys() method to fill a text field , or text area. In order to do that, you need to find the element first using above mentioned guidelines. Here is the sample code to fill the text field.
WebElement textFieldWe = driver.findElement(By.name("First Name"));
textFieldWe.sendKeys("speaking cs");
or
you can combine both two lines like this,
driver.findElement(By.name("FirstName")).sendKeys("speaking cs"));
How to click an Element such as Link, Button Using Selenium ?
Use the click() method of WebElement. Follow the same rules for finding WebElement in the page.
WebElement buttonWe = driver.findElement(By.name("Publish"));
buttonWe.click();
or
combine the above lines like below
driver.findElement(By.name("Publish")).click();
How to submit the Form using Selenium ?
clicking the submit button in the forms won't push your form's data to server. You need to use submit() method of WebElement.
WebElement submitButtonWe = driver.findElement(By.name("submit"));
submitButtonWe.submit();
or
driver.findElement(By.name("submit")).submit();
How to retrieve Attribute from the Element ?
if an Element is associated with Attributes and if you want to get that attribute use getAttribute() method of WebElement.
Eg: <a href = "www.speakingcs.com">speakingcs</a>
first find the anchor tag
WebElement anchorWe = driver.findElement(By.tagName("a"));
System.out.println("url is " + anchorWe.getAttribute("href"));
Retrieving the text associated with the WebElement :
use getText() method to retrieve text of an element. The code snippet is below.
WebElement anchorWe = driver.findElement(By.tagName("a"));
anchorWe.getText(); // gives result as "speakingcs".
How to find InnerHtml and OuterHtml of an Element ?
use getAttribute() method and pass "innerHTML" as argument to get InnterHtml of the element and pass "outerHTML" as argument to get outerHtml. Here is the code.
WebElement anchorWe = driver.findElement(By.tagName("a"));
anchorWe.getAttribute("innerHTML"); // result is <a href = "www.speakingcs.com">speakingcs</a>
anchorWe.getAttribute("outerHTML"); // result will be html code of it's parent tag.
This comment has been removed by a blog administrator.
ReplyDelete