0% found this document useful (0 votes)
67 views11 pages

Selenium Coding Exercises for QA

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)
67 views11 pages

Selenium Coding Exercises for QA

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

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 [Link] and verify that title is Google and also verify that it is redirected to [Link]
1 package nextgenerationautomation;
2
3 import [Link];
4
5 public class SeleniumScript1
6 {
7 public static void main(String[] args)
8 {
9 FirefoxDriver driver=new FirefoxDriver();
10 [Link]("[Link]
11 String title=[Link]();
12 if([Link]("Google"))
13 {
14 [Link]("Pass:Title is Google");
15 }
16 else
17 {
18 [Link]("Fail:Title is not Google: actual title is"+title);
19 }
20
21 String url=[Link]();
22 if([Link]("[Link]"))
23 {
24 [Link]("Pass: url has [Link]");
25 }
26 else
27 {
28 [Link]("Fail:url dont have [Link]"+url);
29 }
30 }
31 }

[Link] hosted with ❤ by GitHub view raw

Exercise 2:

Write a script to open [Link] using chrome browser (ChromeDriver)


1 package nextgenerationautomation;
2
3 import [Link];
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 [Link]("[Link]","D:/NGA/[Link]");
11 ChromeDriver driver=new ChromeDriver();
12 [Link]("[Link]
13 [Link]();
14 }
15 }

[Link] hosted with ❤ by GitHub view raw

Exercise 3:

Write a script to open [Link] using internet explorer (InternetExplorerDriver)


1 package nextgenerationautomation;
2
3 import [Link];
4
5 public class SeleniumScript3
6 {
7 public static void main(String[] args)
8 {
9 // Mention here downloaded path of [Link] path
10 [Link]("[Link]","D:/NGA/[Link]");
11 InternetExplorerDriver driver=new InternetExplorerDriver();
12 [Link]("[Link]
13 [Link]();
14 }
15 }

[Link] 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 [Link];
4 import [Link];
5 import [Link];
6 import [Link];
7 import [Link];
8
9 public class SeleniumScript4
10 {
11 public static void main(String[] args)
12 {
13 Scanner sc=new Scanner([Link]);
14 [Link]("Enter browser?GC/FF/IE");
15 String browser=[Link]();
16 WebDriver driver;
17
18 if([Link]("GC"))
19 {
20 [Link]("[Link]","D:/NGA/[Link]");
21 driver=new ChromeDriver();
22 }
23
24 else if([Link]("IE"))
25 {
26 [Link]("[Link]","D:/NGA/[Link]");
27 driver=new InternetExplorerDriver();
28 }
29
30 else
31 {
32 driver=new FirefoxDriver();
33 }
34
35 [Link]("[Link]
36 [Link]([Link]());
37 [Link]([Link]());
38 [Link]();
39 }
40 }

[Link] 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 [Link];
4 import [Link];
5 import [Link];
6
7 public class SeleniumScript5
8 {
9 public static void main(String[] args)
10 {
11 WebDriver driver=new FirefoxDriver();
12 [Link]("[Link]
13 [Link]([Link]("loginButtonq")).click();
14 [Link]([Link]("memberLoginDialogswitchToEmailLink")).click();
15 [Link]([Link]("username")).sendKeys("superuser");
16 [Link]([Link]("pwd")).sendKeys("welcome");
17 [Link]([Link]("memberLoginDialogokButton")).click();
18 }
19 }

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

[Link] 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 [Link];
4 import [Link];
5 import [Link];
6 import [Link];
7 import [Link];
8 import [Link];
9 import [Link];
10 import [Link];
11 import [Link];
12
13 public class SeleniumScript7
14 {
15 public static void main(String[] args)
16 {
17 WebDriver driver=new FirefoxDriver();
18 [Link]("Enter URL here to verify");
19 WebElement listBox = [Link]([Link]("country"));
20 Select select=new Select(listBox);
21 List<WebElement> allOptions = [Link]();
22 ArrayList<String> allText=new ArrayList<String>();
23
24 for(int i=0;i<[Link]();i++)
25 {
26 String text = [Link](i).getText();
27 [Link](text);
28 }
29
30 [Link](allText);
31 for(String s:allText)
32 {
33 [Link](s);
34 }
35 }
36 }

[Link] hosted with ❤ by GitHub view raw

Exercise 8:

Write a script to print all the [Link] duplicates add entry only once.
Hint: Use HashSet
1 package nextgenerationautomation;
2
3 import [Link];
4 import [Link];
5 import [Link];
6 import [Link];
7 import [Link];
8 import [Link];
9 import [Link];
10
11 public class SeleniumScript8
12 {
13 public static void main(String[] args)
14 {
15 WebDriver driver=new FirefoxDriver();
16 [Link]("Enter URL here to verify");
17 WebElement listBox = [Link]([Link]("country"));
18
19 Select select=new Select(listBox);
20 List<WebElement> allOptions = [Link]();
21 HashSet<String> allText=new HashSet<String>();
22
23 for(int i=0;i<[Link]();i++)
24 {
25 String text = [Link](i).getText();
26 [Link](text);
27 }
28
29 for(Object o:allText)
30 {
31 [Link](o);
32 }
33 }
34 }

[Link] 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 [Link];
4 import [Link];
5 import [Link];
6
7 class SeleniumScript9
8 {
9 public static void main(String[] args)
10 {
11 WebDriver driver=new FirefoxDriver();
12 [Link]("[Link]
13 Set<String> allWH=[Link]();
14 int count=[Link]();
15 [Link](count);
16 for(String wh:allWH)
17 {
18 [Link]().window(wh);
19 String title=[Link]();
20 [Link](title);
21 [Link]();
22 }
23 }
24 }

[Link] 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 ([Link]("css_")) {
14 String s = [Link]("css_", "");
15 return [Link]([Link](s));
16 } else if ([Link]("xpath_")) {
17 return [Link]([Link]([Link]("xpath_", "")));
18 } else if ([Link]("id_")) {
19 return [Link]([Link]([Link]("id_", "")));
20 } else if ([Link]("name_")) {
21 return [Link]([Link]([Link]("name_", "")));
22 } else if ([Link]("link_")) {
23 return [Link]([Link]([Link]("link_", "")));
24 } else if ([Link]("partiallink_")) {
25 return [Link]([Link]([Link]("partiallink_", "")));
26 } else if ([Link]("class_")) {
27 return [Link]([Link]([Link]("class_", "")));
28 } else if ([Link]("tag_")) {
29 return [Link]([Link]([Link]("tag_", "")));
30 } else
31 return null;
32 } catch (Exception e) {
33 [Link](false, [Link]());
34 return null;
35 }
36 }

[Link] 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 ([Link]("css_")) {
17 WebElement we = [Link](
18 [Link]([Link]([Link]("css_", ""))));
19 return we;
20 } else if ([Link]("xpath_")) {
21 WebElement we = [Link](
22 [Link]([Link]([Link]("xpath_", ""))));
23 return we;
24 } else if ([Link]("id_")) {
25 WebElement we = [Link]([Link]([Link]([Link]("
26 return we;
27 } else if ([Link]("name_")) {
28 WebElement we = [Link]([Link]([Link]([Link]
29 return we;
30 } else if ([Link]("link_")) {
31 WebElement we = [Link]([Link]([Link]([Link]
32 return we;
33 } else if ([Link]("partial_")) {
34 WebElement we = [Link]([Link]([Link](locatorP
35 return we;
36 } else if ([Link]("class_")) {
37 WebElement we = [Link]([Link]([Link]([Link]
38 return we;
39 } else if ([Link]("tag_")) {
40 WebElement we = [Link]([Link]([Link]([Link]
41 return we;
42 } else
43 return null;
44 } catch (Exception e) {
45 [Link](false, [Link]());
46 return null;
47 }
48 }

[Link] hosted with ❤ by GitHub view raw

#NGAutomation

You might also like