Selenium Coding Exercises
Selenium Coding Exercises
Exercise 1:
Write a script to open google.com and verify that title is Google and also verify that it is redirected to google.co.in
1 package nextgenerationautomation;
2
3 import org.openqa.selenium.firefox.FirefoxDriver;
4
5 public class SeleniumScript1
6 {
7 public static void main(String[] args)
8 {
9 FirefoxDriver driver=new FirefoxDriver();
10 driver.get("http://www.google.com");
11 String title=driver.getTitle();
12 if(title.equals("Google"))
13 {
14 System.out.println("Pass:Title is Google");
15 }
16 else
17 {
18 System.out.println("Fail:Title is not Google: actual title is"+title);
19 }
20
21 String url=driver.getCurrentUrl();
22 if(url.contains("google.co.in"))
23 {
24 System.out.println("Pass: url has co.in");
25 }
26 else
27 {
28 System.out.println("Fail:url dont have co.in"+url);
29 }
30 }
31 }
Exercise 2:
Exercise 3:
Exercise 4:
NGA Overseas Hiring Model Live Now. Model helps connect QA Automation Engineers directly with Overseas Employers for high growth
Software Testing Jobs both Remote and Onsite. To know more about the offered service, click here.
Exercise 5:
Write script to login Next Generation Automation
1 package nextgenerationautomation;
2
3 import org.openqa.selenium.By;
4 import org.openqa.selenium.WebDriver;
5 import org.openqa.selenium.firefox.FirefoxDriver;
6
7 public class SeleniumScript5
8 {
9 public static void main(String[] args)
10 {
11 WebDriver driver=new FirefoxDriver();
12 driver.get("https://www.nextgenerationautomation.com");
13 driver.findElement(By.id("loginButtonq")).click();
14 driver.findElement(By.id("memberLoginDialogswitchToEmailLink")).click();
15 driver.findElement(By.id("username")).sendKeys("superuser");
16 driver.findElement(By.name("pwd")).sendKeys("welcome");
17 driver.findElement(By.id("memberLoginDialogokButton")).click();
18 }
19 }
Exercise 6:
Exercise 7:
Exercise 8:
Write a script to print all the options.For duplicates add entry only once.
Hint: Use HashSet
1 package nextgenerationautomation;
2
3 import java.util.HashSet;
4 import java.util.List;
5 import org.openqa.selenium.By;
6 import org.openqa.selenium.WebDriver;
7 import org.openqa.selenium.WebElement;
8 import org.openqa.selenium.firefox.FirefoxDriver;
9 import org.openqa.selenium.support.ui.Select;
10
11 public class SeleniumScript8
12 {
13 public static void main(String[] args)
14 {
15 WebDriver driver=new FirefoxDriver();
16 driver.get("Enter URL here to verify");
17 WebElement listBox = driver.findElement(By.name("country"));
18
19 Select select=new Select(listBox);
20 List<WebElement> allOptions = select.getOptions();
21 HashSet<String> allText=new HashSet<String>();
22
23 for(int i=0;i<allOptions.size();i++)
24 {
25 String text = allOptions.get(i).getText();
26 allText.add(text);
27 }
28
29 for(Object o:allText)
30 {
31 System.out.println(o);
32 }
33 }
34 }
Exercise 9:
Write a script to close all the browsers without using quit() method.
1 package nextgenerationautomation;
2
3 import java.util.Set;
4 import org.openqa.selenium.WebDriver;
5 import org.openqa.selenium.firefox.FirefoxDriver;
6
7 class SeleniumScript9
8 {
9 public static void main(String[] args)
10 {
11 WebDriver driver=new FirefoxDriver();
12 driver.get("http://www.timesofindia.indiatimes.com//");
13 Set<String> allWH=driver.getWindowHandles();
14 int count=allWH.size();
15 System.out.println(count);
16 for(String wh:allWH)
17 {
18 driver.switchTo().window(wh);
19 String title=driver.getTitle();
20 System.out.println(title);
21 driver.close();
22 }
23 }
24 }
Exercise 10:
Write generic method in selenium to handle all locators and return web element for any locator.
1 /**
2 ****************************************************************************************
3 * Find element based on the input locator path
4 *
5 * @param locatorPath
6 * This is locator path in String format
7 * @return WebElement
8 * Example: locatorPath = "xpath_//table[@class='dataTable']/tbody/tr"
9 ***************************************************************************************
10 */
11 public static WebElement getAnyElementByAnyLocator(String locatorPath) {
12 try {
13 if (locatorPath.startsWith("css_")) {
14 String s = locatorPath.replace("css_", "");
15 return driver.findElement(By.cssSelector(s));
16 } else if (locatorPath.startsWith("xpath_")) {
17 return driver.findElement(By.xpath(locatorPath.replace("xpath_", "")));
18 } else if (locatorPath.startsWith("id_")) {
19 return driver.findElement(By.id(locatorPath.replace("id_", "")));
20 } else if (locatorPath.startsWith("name_")) {
21 return driver.findElement(By.name(locatorPath.replace("name_", "")));
22 } else if (locatorPath.startsWith("link_")) {
23 return driver.findElement(By.linkText(locatorPath.replace("link_", "")));
24 } else if (locatorPath.startsWith("partiallink_")) {
25 return driver.findElement(By.partialLinkText(locatorPath.replace("partiallink_", "")));
26 } else if (locatorPath.startsWith("class_")) {
27 return driver.findElement(By.xpath(locatorPath.replace("class_", "")));
28 } else if (locatorPath.startsWith("tag_")) {
29 return driver.findElement(By.tagName(locatorPath.replace("tag_", "")));
30 } else
31 return null;
32 } catch (Exception e) {
33 Assert.assertTrue(false, e.getMessage());
34 return null;
35 }
36 }
Exercise 11:
Write generic method in selenium to handle all locators containing dynamic wait and return web element for any locator.
1 /**
2 ******************************************************************************************
3 * Find element based on the input locator path and wait time.
4 *
5 * @param locatorPath
6 * This is locator path in String format.
7 * @param waitTime
8 * This is wait time in Seconds Long format.
9 * @return WebElement.
10 * Example: locatorPath = "xpath_//table[@class='dataTable']/tbody/tr"
11 *****************************************************************************************
12 */
13 public static WebElement getAnyElementByAnyLocatorSpecificWait(String locatorPath, long waitTime) {
14 try {
15 WebDriverWait w = new WebDriverWait(driver, waitTime);
16 if (locatorPath.startsWith("css_")) {
17 WebElement we = w.until(
18 ExpectedConditions.presenceOfElementLocated(By.cssSelector(locatorPath.replace("css_", ""))));
19 return we;
20 } else if (locatorPath.startsWith("xpath_")) {
21 WebElement we = w.until(
22 ExpectedConditions.presenceOfElementLocated(By.xpath(locatorPath.replace("xpath_", ""))));
23 return we;
24 } else if (locatorPath.startsWith("id_")) {
25 WebElement we = w.until(ExpectedConditions.presenceOfElementLocated(By.id(locatorPath.replace("
26 return we;
27 } else if (locatorPath.startsWith("name_")) {
28 WebElement we = w.until(ExpectedConditions.presenceOfElementLocated(By.name(locatorPath.replace
29 return we;
30 } else if (locatorPath.startsWith("link_")) {
31 WebElement we = w.until(ExpectedConditions.presenceOfElementLocated(By.linkText(locatorPath.rep
32 return we;
33 } else if (locatorPath.startsWith("partial_")) {
34 WebElement we = w.until(ExpectedConditions.presenceOfElementLocated(By.partialLinkText(locatorP
35 return we;
36 } else if (locatorPath.startsWith("class_")) {
37 WebElement we = w.until(ExpectedConditions.presenceOfElementLocated(By.className(locatorPath.re
38 return we;
39 } else if (locatorPath.startsWith("tag_")) {
40 WebElement we = w.until(ExpectedConditions.presenceOfElementLocated(By.tagName(locatorPath.repl
41 return we;
42 } else
43 return null;
44 } catch (Exception e) {
45 Assert.assertTrue(false, e.getMessage());
46 return null;
47 }
48 }
#NGAutomation