0% found this document useful (0 votes)
19 views

Selenium Coding Exercises

The document contains a series of Selenium coding exercises aimed at preparing QA Automation Engineers for interviews and practical applications. It includes scripts for various tasks such as opening web pages, verifying titles, handling different browsers, and managing web elements using different locators. Additionally, it introduces a hiring model for connecting QA Automation Engineers with overseas employers for job opportunities.

Uploaded by

ambikavarmak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Selenium Coding Exercises

The document contains a series of Selenium coding exercises aimed at preparing QA Automation Engineers for interviews and practical applications. It includes scripts for various tasks such as opening web pages, verifying titles, handling different browsers, and managing web elements using different locators. Additionally, it introduces a hiring model for connecting QA Automation Engineers with overseas employers for job opportunities.

Uploaded by

ambikavarmak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

NextGenerationAutomation

Selenium Coding Exercises


lps QA to walk through sample selenium coding exercise which will be helpful both for interviews preparation and working as Automation
QA to use shared code in live projects.

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 }

SeleniumScript1.java hosted with ❤ by GitHub view raw

Exercise 2:

Write a script to open google.co.in using chrome browser (ChromeDriver)


1 package nextgenerationautomation;
2
3 import org.openqa.selenium.chrome.ChromeDriver;
4
5 public class SeleniumScript2
6 {
7 public static void main(String[] args) throws InterruptedException
8 {
9 // Mention here location of downloaded chrome driver path
10 System.setProperty("webdriver.chrome.driver","D:/NGA/chromedriver.exe");
11 ChromeDriver driver=new ChromeDriver();
12 driver.get("https://www.google.co.in");
13 driver.close();
14 }
15 }

SeleniumScript2.java hosted with ❤ by GitHub view raw

Exercise 3:

Write a script to open google.co.in using internet explorer (InternetExplorerDriver)


1 package nextgenerationautomation;
2
3 import org.openqa.selenium.ie.InternetExplorerDriver;
4
5 public class SeleniumScript3
6 {
7 public static void main(String[] args)
8 {
9 // Mention here downloaded path of IEDriverServer.exe path
10 System.setProperty("webdriver.ie.driver","D:/NGA/IEDriverServer.exe");
11 InternetExplorerDriver driver=new InternetExplorerDriver();
12 driver.get("https://www.google.co.in");
13 driver.close();
14 }
15 }

SeleniumScript3.java hosted with ❤ by GitHub view raw

Exercise 4:

Write a script to create browser instance based on browser name


1 package nextgenerationautomation;
2
3 import java.util.Scanner;
4 import org.openqa.selenium.WebDriver;
5 import org.openqa.selenium.chrome.ChromeDriver;
6 import org.openqa.selenium.firefox.FirefoxDriver;
7 import org.openqa.selenium.ie.InternetExplorerDriver;
8
9 public class SeleniumScript4
10 {
11 public static void main(String[] args)
12 {
13 Scanner sc=new Scanner(System.in);
14 System.out.println("Enter browser?GC/FF/IE");
15 String browser=sc.next();
16 WebDriver driver;
17
18 if(browser.equals("GC"))
19 {
20 System.setProperty("webdriver.chrome.driver","D:/NGA/chromedriver.exe");
21 driver=new ChromeDriver();
22 }
23
24 else if(browser.equals("IE"))
25 {
26 System.setProperty("webdriver.ie.driver","D:/NGA/IEDriverServer.exe");
27 driver=new InternetExplorerDriver();
28 }
29
30 else
31 {
32 driver=new FirefoxDriver();
33 }
34
35 driver.get("https://www.google.co.in");
36 System.out.println(driver.getTitle());
37 System.out.println(driver.getCurrentUrl());
38 driver.close();
39 }
40 }

SeleniumScript4.java hosted with ❤ by GitHub view raw

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 }

SeleniumScript5.java hosted with ❤ by GitHub view raw

Exercise 6:

Write a script to search for specified option in the listbox


1 package nextgenerationautomation;
2
3 import java.util.List;
4 import java.util.Scanner;
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 SeleniumScript6
12 {
13 public static void main(String[] args)
14 {
15 System.out.println("Enter option to search:");
16 Scanner sc=new Scanner(System.in);
17 String eText=sc.next();
18 int found=0;
19 System.out.println("Searching....");
20
21 WebDriver driver=new FirefoxDriver();
22 driver.get("Enter URL Here to verify");
23 WebElement listBox = driver.findElement(By.name("country"));
24 Select select=new Select(listBox);
25 List<WebElement> allOptions = select.getOptions();
26
27 for(int i=0;i<allOptions.size();i++)
28 {
29 String atext = allOptions.get(i).getText();
30 if(atext.equals(eText))
31 {
32 found++;
33 }
34 }
35
36 if(found==0)
37 {
38 System.out.println(eText+" is not found"); //No matching
39 }
40 else if(found==1) // if found >1 then duplicates
41 {
42 System.out.println(eText+" is found"); //matching found
43 }
44 else
45 {
46 System.out.println(eText+ "is duplicate");
47 }
48 driver.close();
49 }
50 }

SeleniumScript6.java hosted with ❤ by GitHub view raw

Exercise 7:

Write a script to print the content of list in sorted order.


1 package nextgenerationautomation;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.Scanner;
7 import org.openqa.selenium.By;
8 import org.openqa.selenium.WebDriver;
9 import org.openqa.selenium.WebElement;
10 import org.openqa.selenium.firefox.FirefoxDriver;
11 import org.openqa.selenium.support.ui.Select;
12
13 public class SeleniumScript7
14 {
15 public static void main(String[] args)
16 {
17 WebDriver driver=new FirefoxDriver();
18 driver.get("Enter URL here to verify");
19 WebElement listBox = driver.findElement(By.name("country"));
20 Select select=new Select(listBox);
21 List<WebElement> allOptions = select.getOptions();
22 ArrayList<String> allText=new ArrayList<String>();
23
24 for(int i=0;i<allOptions.size();i++)
25 {
26 String text = allOptions.get(i).getText();
27 allText.add(text);
28 }
29
30 Collections.sort(allText);
31 for(String s:allText)
32 {
33 System.out.println(s);
34 }
35 }
36 }

SeleniumScript7.java hosted with ❤ by GitHub view raw

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 }

SeleniumScript8.java hosted with ❤ by GitHub view raw

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 }

SeleniumScript9.java hosted with ❤ by GitHub view raw

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 }

SeleniumScript10.java hosted with ❤ by GitHub view raw

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 }

SeleniumScript11.java hosted with ❤ by GitHub view raw

#NGAutomation

You might also like