ALL SLIPS CODE : Software Testing (Selenium) practical, slip-wise

Each slip has: question, commands, folder structure, then every file with its path and code.

CONTENTS

  Slip  1 : Download a file
  Slip  2 : Take a screenshot
  Slip  3 : Signup button and registration page
  Slip  4 : Verify title, Wikipedia icon and search button
  Slip  5 : Search three keyword combinations
  Slip  6 : Parallel execution in TestNG
  Slip  7 : Cross browser testing with TestNG
  Slip  8 : Page Object Model without Page Factory
  Slip  9 : Count and print hyperlinks
  Slip 10 : Find broken (dead) links
  Slip 11 : Count radio buttons and create testng.xml
  Slip 12 : invocationCount in TestNG
  Slip 13 : Verify title and Wikipedia widget
  Slip 14 : Register a new user
  Slip 15 : Handle basic HTML controls
  Slip 16 : Count radio buttons with a TestNG XML file
  Slip 17 : Log in and verify the Log out link
  Slip 18 : Download a file with Chrome options
  Slip 19 : Screenshot of a web page
  Slip 20 : Repeat a search for three keywords
  Slip 21 : Page Object Model with Page Factory

################################################################################
# SLIP 1 : Download a file
################################################################################

QUESTION

Create a Maven project using Selenium WebDriver.

1. Launch the Chrome browser.
2. Get the URL http://omayo.blogspot.com/p/page7.html
3. Download a file in the Selenium project.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip1" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip1
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test-compile exec:java

FOLDER STRUCTURE

slip1/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- downloadFile.java
`-- pom.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip1/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip1</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test-compile exec:java runs the main method -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>practical_Exam.slips.downloadFile</mainClass>
          <classpathScope>test</classpathScope>
          <cleanupDaemonThreads>false</cleanupDaemonThreads>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : downloadFile.java
PATH : slip1/src/test/java/practical_Exam/slips/downloadFile.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import java.io.File;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class downloadFile {

	public static void main(String[] args) {
		// files are saved in the "downloads" folder of the project
		File folder = new File(System.getProperty("user.dir"), "downloads");
		folder.mkdirs();

		Map<String, Object> prefs = new HashMap<>();
		prefs.put("download.default_directory", folder.getAbsolutePath());
		prefs.put("download.prompt_for_download", false);
		prefs.put("download.directory_upgrade", true);

		ChromeOptions options = new ChromeOptions();
		options.setExperimentalOption("prefs", prefs);

		WebDriver driver = new ChromeDriver(options);
		try {
			driver.get("http://omayo.blogspot.com/p/page7.html");
			driver.findElement(By.linkText("ZIP file")).click();

			// wait until the download is complete, otherwise the browser closes too early
			new WebDriverWait(driver, Duration.ofSeconds(60)).until(d -> {
				File[] zips = folder.listFiles((dir, name) -> name.endsWith(".zip"));
				return zips != null && zips.length > 0;
			});
			System.out.println("File downloaded in: " + folder.getAbsolutePath());
		} finally {
			driver.quit();
		}
	}

}

################################################################################
# SLIP 2 : Take a screenshot
################################################################################

QUESTION

Create a Maven project using Selenium WebDriver.

1. Launch the Chrome browser.
2. Get the URL https://garwarecollege.mespune.in/
3. Take screenshots using Selenium WebDriver.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip2" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip2
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test-compile exec:java

FOLDER STRUCTURE

slip2/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- screenshot.java
`-- pom.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip2/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip2</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test-compile exec:java runs the main method -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>practical_Exam.slips.screenshot</mainClass>
          <classpathScope>test</classpathScope>
          <cleanupDaemonThreads>false</cleanupDaemonThreads>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : screenshot.java
PATH : slip2/src/test/java/practical_Exam/slips/screenshot.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class screenshot {

	public static void main(String[] args) throws Exception {
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.get("https://garwarecollege.mespune.in/");

		File source = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
		File target = new File("screenshots.png");
		Files.copy(source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
		System.out.println("Screenshot saved: " + target.getAbsolutePath());

		driver.quit();
	}

}

################################################################################
# SLIP 3 : Signup button and registration page
################################################################################

QUESTION

Create a Maven project using Selenium WebDriver.

1. Launch the Chrome browser.
2. Get the URL https://www.spicejet.com/
3. Click on the Signup button.
4. Verify the registration page.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip3" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip3
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test-compile exec:java

FOLDER STRUCTURE

slip3/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- verifySignup.java
`-- pom.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip3/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip3</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test-compile exec:java runs the main method -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>practical_Exam.slips.verifySignup</mainClass>
          <classpathScope>test</classpathScope>
          <cleanupDaemonThreads>false</cleanupDaemonThreads>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : verifySignup.java
PATH : slip3/src/test/java/practical_Exam/slips/verifySignup.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class verifySignup {

	public static void main(String[] args) {
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
		try {
			driver.get("https://www.spicejet.com/");
			String parent = driver.getWindowHandle();

			// "Join SpiceClub" is the signup link; it opens the registration page in a new tab
			WebElement signup = wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("Join SpiceClub")));
			// click with JavaScript, because a banner on this site can cover the link
			((JavascriptExecutor) driver).executeScript("arguments[0].click();", signup);

			// switch to the new tab
			wait.until(ExpectedConditions.numberOfWindowsToBe(2));
			for (String handle : driver.getWindowHandles()) {
				if (!handle.equals(parent)) {
					driver.switchTo().window(handle);
				}
			}

			wait.until(ExpectedConditions.titleContains("SpiceClub"));
			System.out.println("Page title: " + driver.getTitle());
			System.out.println("Signup page verification successful");
		} catch (Exception e) {
			System.out.println("Signup page verification failed: " + e.getMessage());
		} finally {
			driver.quit();
		}
	}

}

################################################################################
# SLIP 4 : Verify title, Wikipedia icon and search button
################################################################################

QUESTION

Create a Maven project and add all dependencies in the pom.xml file.

1. Launch the Chrome browser.
2. Get the URL https://testautomationpractice.blogspot.com/
3. Verify the title of the web page.
4. Verify the presence of the Wikipedia icon on the web page.
5. Verify the presence of the Wikipedia search button on the web page.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip4" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip4
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test-compile exec:java

FOLDER STRUCTURE

slip4/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- verifyPageTitle.java
`-- pom.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip4/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip4</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test-compile exec:java runs the main method -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>practical_Exam.slips.verifyPageTitle</mainClass>
          <classpathScope>test</classpathScope>
          <cleanupDaemonThreads>false</cleanupDaemonThreads>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : verifyPageTitle.java
PATH : slip4/src/test/java/practical_Exam/slips/verifyPageTitle.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class verifyPageTitle {

	public static void main(String[] args) {
		WebDriver driver = new ChromeDriver();
		driver.get("https://testautomationpractice.blogspot.com/");

		if (driver.getTitle().contains("Automation Testing Practice")) {
			System.out.println("Title verified successfully");
		} else {
			System.out.println("Title verification failed");
		}

		// findElements gives an empty list (no exception) when the element is missing
		if (!driver.findElements(By.className("wikipedia-icon")).isEmpty()) {
			System.out.println("Wikipedia icon is present");
		} else {
			System.out.println("Wikipedia icon is not present");
		}

		if (!driver.findElements(By.className("wikipedia-search-button")).isEmpty()) {
			System.out.println("Wikipedia search button is present");
		} else {
			System.out.println("Wikipedia search button is not present");
		}

		driver.quit();
	}

}

################################################################################
# SLIP 5 : Search three keyword combinations
################################################################################

QUESTION

Create a Maven project using Selenium WebDriver.

1. Launch the Chrome browser.
2. Search the first keyword combination.
3. Repeat step 2 for the other two keyword combinations.

| S.no | Country | Monument |
|---|---|---|
| 1 | India | Qutub Minar |
| 2 | Agra | Taj Mahal |
| 3 | Hyderabad | Charminar |

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip5" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip5
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test-compile exec:java

FOLDER STRUCTURE

slip5/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- multipleKeywordSearch.java
`-- pom.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip5/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip5</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test-compile exec:java runs the main method -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>practical_Exam.slips.multipleKeywordSearch</mainClass>
          <classpathScope>test</classpathScope>
          <cleanupDaemonThreads>false</cleanupDaemonThreads>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : multipleKeywordSearch.java
PATH : slip5/src/test/java/practical_Exam/slips/multipleKeywordSearch.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class multipleKeywordSearch {

	public static void main(String[] args) {
		String[] keywords = { "India Qutub Minar", "Agra Taj Mahal", "Hyderabad Charminar" };

		WebDriver driver = new ChromeDriver();
		// 2 minutes: if Google shows a captcha, solve it by hand in the browser
		WebDriverWait wait = new WebDriverWait(driver, Duration.ofMinutes(2));

		for (String keyword : keywords) {
			driver.get("https://www.google.com/");
			wait.until(ExpectedConditions.elementToBeClickable(By.name("q"))).sendKeys(keyword, Keys.ENTER);
			// "/search" is in the address of the result page, "/sorry" is the captcha page
			wait.until(d -> d.getCurrentUrl().contains("/search?"));
			System.out.println("Searched: " + keyword);
		}

		driver.quit();
	}

}

################################################################################
# SLIP 6 : Parallel execution in TestNG
################################################################################

QUESTION

Create a Maven project using Selenium WebDriver. Write a test case in Java and execute it in TestNG using the parallel attribute.

1. Launch the Chrome browser.
2. Get the URL https://www.saucedemo.com/
3. Verify the page title.
4. Verify that the website logo is displayed.
5. Display the URL.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip6" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip6
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test

FOLDER STRUCTURE

slip6/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- sauceDemoTest.java
|-- pom.xml
`-- testng.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip6/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip6</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test runs the suite in testng.xml -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.2.5</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : sauceDemoTest.java
PATH : slip6/src/test/java/practical_Exam/slips/sauceDemoTest.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class sauceDemoTest {

	// every test opens its own browser, so the tests can run in parallel
	private WebDriver openSite() {
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.saucedemo.com/");
		return driver;
	}

	@Test
	public void verifyTitle() {
		WebDriver driver = openSite();
		try {
			Assert.assertEquals(driver.getTitle(), "Swag Labs");
			System.out.println("Title verified");
		} finally {
			driver.quit();
		}
	}

	@Test
	public void verifyLogo() {
		WebDriver driver = openSite();
		try {
			Assert.assertTrue(driver.findElement(By.className("login_logo")).isDisplayed());
			System.out.println("Logo is displayed");
		} finally {
			driver.quit();
		}
	}

	@Test
	public void displayUrl() {
		WebDriver driver = openSite();
		try {
			System.out.println("URL: " + driver.getCurrentUrl());
		} finally {
			driver.quit();
		}
	}
}

--------------------------------------------------------------------------------
FILE : testng.xml
PATH : slip6/testng.xml
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods" thread-count="3">
  <test name="Test">
    <classes>
      <class name="practical_Exam.slips.sauceDemoTest"/>
    </classes>
  </test>
</suite>

################################################################################
# SLIP 7 : Cross browser testing with TestNG
################################################################################

QUESTION

Create a Maven project using Selenium WebDriver.

1. Launch the Chrome browser.
2. Get the URL https://www.google.com/
3. Get the URL https://firebox.com/ (the program opens https://www.firefox.com/).
4. Handle cross browser testing in Selenium with TestNG.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip7" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip7
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test

FOLDER STRUCTURE

slip7/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- crossBrowserTesting.java
|-- pom.xml
`-- testng.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip7/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip7</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test runs the suite in testng.xml -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.2.5</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : crossBrowserTesting.java
PATH : slip7/src/test/java/practical_Exam/slips/crossBrowserTesting.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class crossBrowserTesting {

	@Test
	@Parameters("browser")
	public void crossBrowser(String browser) {
		WebDriver driver;
		if (browser.equalsIgnoreCase("Chrome")) {
			driver = new ChromeDriver();
		} else if (browser.equalsIgnoreCase("Firefox")) {
			driver = new FirefoxDriver();
		} else if (browser.equalsIgnoreCase("Edge")) {
			driver = new EdgeDriver();
		} else {
			throw new IllegalArgumentException("Invalid browser name: " + browser);
		}

		try {
			driver.get("https://www.google.com/");
			System.out.println(browser + " : " + driver.getTitle());
			driver.get("https://www.firefox.com/");
			System.out.println(browser + " : " + driver.getTitle());
		} finally {
			driver.quit();
		}
	}
}

--------------------------------------------------------------------------------
FILE : testng.xml
PATH : slip7/testng.xml
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" thread-count="3">
  <test name="ChromeTest">
    <parameter name="browser" value="Chrome"/>
    <classes>
      <class name="practical_Exam.slips.crossBrowserTesting"/>
    </classes>
  </test>
  <test name="FirefoxTest">
    <parameter name="browser" value="Firefox"/>
    <classes>
      <class name="practical_Exam.slips.crossBrowserTesting"/>
    </classes>
  </test>
  <test name="EdgeTest">
    <parameter name="browser" value="Edge"/>
    <classes>
      <class name="practical_Exam.slips.crossBrowserTesting"/>
    </classes>
  </test>
</suite>

################################################################################
# SLIP 8 : Page Object Model without Page Factory
################################################################################

QUESTION

Create a Maven project using the Page Object Model (POM).

1. Launch the Chrome browser.
2. Get the URL https://www.saucedemo.com/
3. Log in to the saucedemo page without Page Factory (use By and findElement()).

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip8" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip8
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test

FOLDER STRUCTURE

slip8/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   |-- loginPage.java
|                   `-- loginTest.java
|-- pom.xml
`-- testng.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip8/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip8</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test runs the suite in testng.xml -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.2.5</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : loginPage.java
PATH : slip8/src/test/java/practical_Exam/slips/loginPage.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

// Page Object: locators and actions of the login page
public class loginPage {

	WebDriver driver;

	By uname = By.id("user-name");
	By pass = By.id("password");
	By loginBtn = By.id("login-button");

	loginPage(WebDriver driver) {
		this.driver = driver;
	}

	public void login(String username, String password) {
		driver.findElement(uname).sendKeys(username);
		driver.findElement(pass).sendKeys(password);
		driver.findElement(loginBtn).click();
	}

}

--------------------------------------------------------------------------------
FILE : loginTest.java
PATH : slip8/src/test/java/practical_Exam/slips/loginTest.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class loginTest {

	@Test
	public void LoginTest() {
		WebDriver driver = new ChromeDriver();
		try {
			driver.get("https://www.saucedemo.com/");
			loginPage page = new loginPage(driver);
			// demo account shown on the saucedemo login page
			page.login("standard_user", "secret_sauce");

			Assert.assertTrue(driver.getCurrentUrl().contains("inventory"), "Login failed");
			System.out.println("Login successful");
		} finally {
			driver.quit();
		}
	}

}

--------------------------------------------------------------------------------
FILE : testng.xml
PATH : slip8/testng.xml
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
    <classes>
      <class name="practical_Exam.slips.loginTest"/>
    </classes>
  </test>
</suite>

################################################################################
# SLIP 9 : Count and print hyperlinks
################################################################################

QUESTION

Create a Maven project and add all dependencies in the pom.xml file.

1. Launch the Chrome browser.
2. Get the URL https://www.calculator.net/
3. Count and print the hyperlinks on the web page.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip9" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip9
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test-compile exec:java

FOLDER STRUCTURE

slip9/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- countHyperlinks.java
`-- pom.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip9/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip9</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test-compile exec:java runs the main method -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>practical_Exam.slips.countHyperlinks</mainClass>
          <classpathScope>test</classpathScope>
          <cleanupDaemonThreads>false</cleanupDaemonThreads>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : countHyperlinks.java
PATH : slip9/src/test/java/practical_Exam/slips/countHyperlinks.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class countHyperlinks {

	public static void main(String[] args) {
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.calculator.net/");

		List<WebElement> links = driver.findElements(By.tagName("a"));
		for (WebElement link : links) {
			System.out.println(link.getText() + " -> " + link.getAttribute("href"));
		}
		System.out.println("Count of HyperLinks: " + links.size());

		driver.quit();
	}

}

################################################################################
# SLIP 10 : Find broken (dead) links
################################################################################

QUESTION

Create a Maven project and add all dependencies in the pom.xml file.

1. Launch the Chrome browser.
2. Get the URL http://www.deadlinkcity.com/
3. Find the broken (dead) links on the web page.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip10" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip10
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test-compile exec:java

FOLDER STRUCTURE

slip10/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- deadLinksCount.java
`-- pom.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip10/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip10</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test-compile exec:java runs the main method -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>practical_Exam.slips.deadLinksCount</mainClass>
          <classpathScope>test</classpathScope>
          <cleanupDaemonThreads>false</cleanupDaemonThreads>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : deadLinksCount.java
PATH : slip10/src/test/java/practical_Exam/slips/deadLinksCount.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import java.net.HttpURLConnection;
import java.net.URI;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class deadLinksCount {

	public static void main(String[] args) {
		// this site works only on http, so stop Chrome from changing it to https
		ChromeOptions options = new ChromeOptions();
		options.addArguments("--disable-features=HttpsUpgrades,HttpsFirstBalancedModeAutoEnable");

		WebDriver driver = new ChromeDriver(options);
		driver.get("http://www.deadlinkcity.com/");
		System.out.println("Page title: " + driver.getTitle());

		List<WebElement> links = driver.findElements(By.tagName("a"));
		int broken = 0;

		for (WebElement link : links) {
			String url = link.getAttribute("href");
			if (url == null || !url.startsWith("http")) {
				continue; // empty link, mailto:, javascript: etc.
			}
			try {
				HttpURLConnection connection = (HttpURLConnection) URI.create(url).toURL().openConnection();
				connection.setRequestMethod("HEAD");
				connection.setConnectTimeout(5000);
				connection.setReadTimeout(5000);
				int code = connection.getResponseCode();
				if (code >= 400) {
					broken++;
					System.out.println(url + " is a broken link (" + code + ")");
				} else {
					System.out.println(url + " is not a broken link (" + code + ")");
				}
				connection.disconnect();
			} catch (Exception e) {
				System.out.println("Unable to check: " + url);
			}
		}

		System.out.println("Total links: " + links.size());
		System.out.println("Broken links: " + broken);
		driver.quit();
	}

}

################################################################################
# SLIP 11 : Count radio buttons and create testng.xml
################################################################################

QUESTION

Create a Maven project and add all dependencies in the pom.xml file.

1. Launch the Chrome browser.
2. Get the URL https://www.techlistic.com/p/selenium-practice-form.html
3. Count the number of radio buttons on the web page and create a TestNG XML file.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip11" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip11
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test

FOLDER STRUCTURE

slip11/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- countRadioButtons.java
|-- pom.xml
`-- testng.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip11/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip11</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test runs the suite in testng.xml -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.2.5</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : countRadioButtons.java
PATH : slip11/src/test/java/practical_Exam/slips/countRadioButtons.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class countRadioButtons {

	@Test
	public void countRdBtn() {
		WebDriver driver = new ChromeDriver();
		try {
			driver.get("https://www.techlistic.com/p/selenium-practice-form.html");
			List<WebElement> list = driver.findElements(By.cssSelector("input[type='radio']"));
			System.out.println("Count of Radio Buttons: " + list.size());
		} finally {
			driver.quit();
		}
	}
}

--------------------------------------------------------------------------------
FILE : testng.xml
PATH : slip11/testng.xml
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
    <classes>
      <class name="practical_Exam.slips.countRadioButtons"/>
    </classes>
  </test>
</suite>

################################################################################
# SLIP 12 : invocationCount in TestNG
################################################################################

QUESTION

Create a Maven project using Selenium WebDriver. Write a test case in Java and execute it in TestNG using the invocationCount attribute, which runs a single test case many times.

1. Launch the Chrome browser.
2. Get the URL https://www.google.com/
3. Verify the page title.
4. Verify that the website logo is displayed.
5. Display the URL.
6. Close all windows.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip12" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip12
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test

FOLDER STRUCTURE

slip12/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- invocationCount.java
|-- pom.xml
`-- testng.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip12/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip12</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test runs the suite in testng.xml -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.2.5</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : invocationCount.java
PATH : slip12/src/test/java/practical_Exam/slips/invocationCount.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class invocationCount {

	@Test(invocationCount = 5)
	public void invocCount() {
		WebDriver driver = new ChromeDriver();
		try {
			driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
			driver.get("https://www.google.com/");

			Assert.assertTrue(driver.getTitle().contains("Google"), "Title not verified");
			System.out.println("Title verified");

			// the logo is an image or an svg, depending on the page Google serves
			By logo = By.cssSelector("img[alt='Google'], [aria-label='Google'], svg.lnXdpd, #hplogo");
			Assert.assertTrue(driver.findElement(logo).isDisplayed(), "Logo is not displayed");
			System.out.println("Logo is displayed");

			System.out.println("URL: " + driver.getCurrentUrl());
		} finally {
			driver.quit(); // closes all windows
		}
	}
}

--------------------------------------------------------------------------------
FILE : testng.xml
PATH : slip12/testng.xml
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
    <classes>
      <class name="practical_Exam.slips.invocationCount"/>
    </classes>
  </test>
</suite>

################################################################################
# SLIP 13 : Verify title and Wikipedia widget
################################################################################

QUESTION

Create a Maven project and add all dependencies in the pom.xml file.

1. Launch the Chrome browser.
2. Get the URL https://testautomationpractice.blogspot.com/
3. Verify the title of the web page.
4. Verify the presence of the Wikipedia icon on the web page.
5. Verify the presence of the Wikipedia search button on the web page.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip13" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip13
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test-compile exec:java

FOLDER STRUCTURE

slip13/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- verifyPageTitle.java
`-- pom.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip13/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip13</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test-compile exec:java runs the main method -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>practical_Exam.slips.verifyPageTitle</mainClass>
          <classpathScope>test</classpathScope>
          <cleanupDaemonThreads>false</cleanupDaemonThreads>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : verifyPageTitle.java
PATH : slip13/src/test/java/practical_Exam/slips/verifyPageTitle.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class verifyPageTitle {

	public static void main(String[] args) {
		WebDriver driver = new ChromeDriver();
		driver.get("https://testautomationpractice.blogspot.com/");

		if (driver.getTitle().contains("Automation Testing Practice")) {
			System.out.println("Title verified successfully");
		} else {
			System.out.println("Title verification failed");
		}

		// findElements gives an empty list (no exception) when the element is missing
		if (!driver.findElements(By.className("wikipedia-icon")).isEmpty()) {
			System.out.println("Wikipedia icon is present");
		} else {
			System.out.println("Wikipedia icon is not present");
		}

		if (!driver.findElements(By.className("wikipedia-search-button")).isEmpty()) {
			System.out.println("Wikipedia search button is present");
		} else {
			System.out.println("Wikipedia search button is not present");
		}

		driver.quit();
	}

}

################################################################################
# SLIP 14 : Register a new user
################################################################################

QUESTION

Create a Maven project using Selenium WebDriver.

1. Launch the Chrome browser.
2. Navigate to the URL https://demowebshop.tricentis.com/
3. Click on the Register link.
4. Enter valid personal details and a password.
5. Click the Register button.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip14" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip14
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test-compile exec:java

FOLDER STRUCTURE

slip14/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- Registration.java
`-- pom.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip14/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip14</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test-compile exec:java runs the main method -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>practical_Exam.slips.Registration</mainClass>
          <classpathScope>test</classpathScope>
          <cleanupDaemonThreads>false</cleanupDaemonThreads>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : Registration.java
PATH : slip14/src/test/java/practical_Exam/slips/Registration.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Registration {

	public static void main(String[] args) {
		// the site accepts an email only once, so a new one is made on every run
		String email = "student" + System.currentTimeMillis() + "@example.com";
		String password = "Test@12345";

		WebDriver driver = new ChromeDriver();
		// wait up to 15 seconds for elements, because the next page can load slowly
		driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
		driver.get("https://demowebshop.tricentis.com/");
		driver.findElement(By.linkText("Register")).click();

		driver.findElement(By.id("gender-female")).click();
		driver.findElement(By.id("FirstName")).sendKeys("Test");
		driver.findElement(By.id("LastName")).sendKeys("Student");
		driver.findElement(By.id("Email")).sendKeys(email);
		driver.findElement(By.id("Password")).sendKeys(password);
		driver.findElement(By.id("ConfirmPassword")).sendKeys(password);
		driver.findElement(By.id("register-button")).click();

		By success = By.xpath("//div[@class='result' and contains(., 'Your registration completed')]");
		if (!driver.findElements(success).isEmpty()) {
			System.out.println("Registration successful");
			System.out.println("Email: " + email);
			System.out.println("Password: " + password);
		} else {
			System.out.println("Registration unsuccessful");
		}

		driver.quit();
	}

}

################################################################################
# SLIP 15 : Handle basic HTML controls
################################################################################

QUESTION

Create a Maven project and add all dependencies in the pom.xml file.

1. Launch the Chrome browser.
2. Get the URL https://www.hyrtutorials.com/p/basic-controls.html
3. Handle the basic HTML controls in Selenium WebDriver.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip15" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip15
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test-compile exec:java

FOLDER STRUCTURE

slip15/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- handleHTMLControls.java
`-- pom.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip15/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip15</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test-compile exec:java runs the main method -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>practical_Exam.slips.handleHTMLControls</mainClass>
          <classpathScope>test</classpathScope>
          <cleanupDaemonThreads>false</cleanupDaemonThreads>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : handleHTMLControls.java
PATH : slip15/src/test/java/practical_Exam/slips/handleHTMLControls.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class handleHTMLControls {

	public static void main(String[] args) {
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.get("https://www.hyrtutorials.com/p/basic-controls.html");

		// text boxes
		driver.findElement(By.id("firstName")).sendKeys("Test");
		driver.findElement(By.id("lastName")).sendKeys("Student");
		// radio button
		driver.findElement(By.id("femalerb")).click();
		// check boxes
		driver.findElement(By.id("englishchbx")).click();
		driver.findElement(By.id("hindichbx")).click();
		// email and password
		driver.findElement(By.id("email")).sendKeys("student@example.com");
		driver.findElement(By.id("password")).sendKeys("Test@12345");
		// button
		driver.findElement(By.id("registerbtn")).click();

		if (driver.getPageSource().contains("Registration is Successful")) {
			System.out.println("Registration successful");
		} else {
			System.out.println("Registration unsuccessful");
		}

		driver.findElement(By.id("clearbtn")).click();
		driver.quit();
	}

}

################################################################################
# SLIP 16 : Count radio buttons with a TestNG XML file
################################################################################

QUESTION

Create a Maven project and add all dependencies in the pom.xml file.

1. Launch the Chrome browser.
2. Get the URL https://www.techlistic.com/p/selenium-practice-form.html
3. Count the number of radio buttons on the web page and create a TestNG XML file.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip16" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip16
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test

FOLDER STRUCTURE

slip16/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- countRadioButtons.java
|-- pom.xml
`-- testng.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip16/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip16</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test runs the suite in testng.xml -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.2.5</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : countRadioButtons.java
PATH : slip16/src/test/java/practical_Exam/slips/countRadioButtons.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class countRadioButtons {

	@Test
	public void countRdBtn() {
		WebDriver driver = new ChromeDriver();
		try {
			driver.get("https://www.techlistic.com/p/selenium-practice-form.html");
			List<WebElement> list = driver.findElements(By.cssSelector("input[type='radio']"));
			System.out.println("Count of Radio Buttons: " + list.size());
		} finally {
			driver.quit();
		}
	}
}

--------------------------------------------------------------------------------
FILE : testng.xml
PATH : slip16/testng.xml
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
    <classes>
      <class name="practical_Exam.slips.countRadioButtons"/>
    </classes>
  </test>
</suite>

################################################################################
# SLIP 17 : Log in and verify the Log out link
################################################################################

QUESTION

Create a Maven project using Selenium WebDriver.

1. Launch the Chrome browser.
2. Get the URL https://demowebshop.tricentis.com/
3. Click on the Log in link.
4. Enter a valid registered email address.
5. Enter the corresponding password.
6. Click the Log in button.
7. Verify that the user is successfully logged in.
8. Verify that the Log out link is displayed.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip17" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip17
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test-compile exec:java

FOLDER STRUCTURE

slip17/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- LoginLogout.java
`-- pom.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip17/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip17</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test-compile exec:java runs the main method -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>practical_Exam.slips.LoginLogout</mainClass>
          <classpathScope>test</classpathScope>
          <cleanupDaemonThreads>false</cleanupDaemonThreads>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : LoginLogout.java
PATH : slip17/src/test/java/practical_Exam/slips/LoginLogout.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LoginLogout {

	// CHANGE THESE: use an account you registered on the site (slip 14 makes one)
	static final String EMAIL = "your.email@example.com";
	static final String PASSWORD = "YourPassword";

	public static void main(String[] args) {
		WebDriver driver = new ChromeDriver();
		// wait up to 15 seconds for elements, because the next page can load slowly
		driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
		driver.get("https://demowebshop.tricentis.com/");
		driver.findElement(By.linkText("Log in")).click();
		driver.findElement(By.id("Email")).sendKeys(EMAIL);
		driver.findElement(By.id("Password")).sendKeys(PASSWORD);
		driver.findElement(By.cssSelector(".button-1.login-button")).click();

		// findElements gives an empty list (no exception) when the link is missing
		if (!driver.findElements(By.linkText("Log out")).isEmpty()) {
			System.out.println("Logout is displayed");
			System.out.println("Login verified successfully");
		} else {
			System.out.println("Logout is not displayed");
			System.out.println("Login not verified successfully");
		}
		driver.quit();
	}

}

################################################################################
# SLIP 18 : Download a file with Chrome options
################################################################################

QUESTION

Create a Maven project using Selenium WebDriver.

1. Launch the Chrome browser.
2. Get the URL http://omayo.blogspot.com/p/page7.html
3. Download a file in the Selenium project.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip18" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip18
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test-compile exec:java

FOLDER STRUCTURE

slip18/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- downloadFile.java
`-- pom.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip18/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip18</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test-compile exec:java runs the main method -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>practical_Exam.slips.downloadFile</mainClass>
          <classpathScope>test</classpathScope>
          <cleanupDaemonThreads>false</cleanupDaemonThreads>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : downloadFile.java
PATH : slip18/src/test/java/practical_Exam/slips/downloadFile.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import java.io.File;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class downloadFile {

	public static void main(String[] args) {
		// files are saved in the "downloads" folder of the project
		File folder = new File(System.getProperty("user.dir"), "downloads");
		folder.mkdirs();

		Map<String, Object> prefs = new HashMap<>();
		prefs.put("download.default_directory", folder.getAbsolutePath());
		prefs.put("download.prompt_for_download", false);
		prefs.put("download.directory_upgrade", true);

		ChromeOptions options = new ChromeOptions();
		options.setExperimentalOption("prefs", prefs);

		WebDriver driver = new ChromeDriver(options);
		try {
			driver.get("http://omayo.blogspot.com/p/page7.html");
			driver.findElement(By.linkText("ZIP file")).click();

			// wait until the download is complete, otherwise the browser closes too early
			new WebDriverWait(driver, Duration.ofSeconds(60)).until(d -> {
				File[] zips = folder.listFiles((dir, name) -> name.endsWith(".zip"));
				return zips != null && zips.length > 0;
			});
			System.out.println("File downloaded in: " + folder.getAbsolutePath());
		} finally {
			driver.quit();
		}
	}

}

################################################################################
# SLIP 19 : Screenshot of a web page
################################################################################

QUESTION

Create a Maven project using Selenium WebDriver.

1. Launch the Chrome browser.
2. Get the URL https://garwarecollege.mespune.in/
3. Take screenshots using Selenium.

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip19" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip19
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test-compile exec:java

FOLDER STRUCTURE

slip19/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- screenshot.java
`-- pom.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip19/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip19</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test-compile exec:java runs the main method -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>practical_Exam.slips.screenshot</mainClass>
          <classpathScope>test</classpathScope>
          <cleanupDaemonThreads>false</cleanupDaemonThreads>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : screenshot.java
PATH : slip19/src/test/java/practical_Exam/slips/screenshot.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class screenshot {

	public static void main(String[] args) throws Exception {
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.get("https://garwarecollege.mespune.in/");

		File source = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
		File target = new File("screenshots.png");
		Files.copy(source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
		System.out.println("Screenshot saved: " + target.getAbsolutePath());

		driver.quit();
	}

}

################################################################################
# SLIP 20 : Repeat a search for three keywords
################################################################################

QUESTION

Create a Maven project using Selenium WebDriver.

1. Launch the Chrome browser.
2. Search the first keyword combination.
3. Repeat step 2 for the other two keyword combinations.

| S.no | Country | Monument |
|---|---|---|
| 1 | India | Qutub Minar |
| 2 | Agra | Taj Mahal |
| 3 | Hyderabad | Charminar |

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip20" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip20
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test-compile exec:java

FOLDER STRUCTURE

slip20/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   `-- multipleKeywordSearch.java
`-- pom.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip20/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip20</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test-compile exec:java runs the main method -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>practical_Exam.slips.multipleKeywordSearch</mainClass>
          <classpathScope>test</classpathScope>
          <cleanupDaemonThreads>false</cleanupDaemonThreads>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : multipleKeywordSearch.java
PATH : slip20/src/test/java/practical_Exam/slips/multipleKeywordSearch.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class multipleKeywordSearch {

	public static void main(String[] args) {
		String[] keywords = { "India Qutub Minar", "Agra Taj Mahal", "Hyderabad Charminar" };

		WebDriver driver = new ChromeDriver();
		// 2 minutes: if Google shows a captcha, solve it by hand in the browser
		WebDriverWait wait = new WebDriverWait(driver, Duration.ofMinutes(2));

		for (String keyword : keywords) {
			driver.get("https://www.google.com/");
			wait.until(ExpectedConditions.elementToBeClickable(By.name("q"))).sendKeys(keyword, Keys.ENTER);
			// "/search" is in the address of the result page, "/sorry" is the captcha page
			wait.until(d -> d.getCurrentUrl().contains("/search?"));
			System.out.println("Searched: " + keyword);
		}

		driver.quit();
	}

}

################################################################################
# SLIP 21 : Page Object Model with Page Factory
################################################################################

QUESTION

Create a Maven project using the Page Object Model (POM).

1. Launch the Chrome browser.
2. Get the URL https://www.saucedemo.com/
3. Log in to the saucedemo page with Page Factory (use the @FindBy annotation and PageFactory.initElements()).

COMMANDS

mvn archetype:generate "-DgroupId=practical_Exam" "-DartifactId=slip21" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"
cd slip21
Remove-Item -Recurse src\main, src\test
mkdir src\test\java\practical_Exam\slips
code .
mvn test

FOLDER STRUCTURE

slip21/
|-- src/
|   `-- test/
|       `-- java/
|           `-- practical_Exam/
|               `-- slips/
|                   |-- LoginPageFactory.java
|                   `-- LoginTestFactory.java
|-- pom.xml
`-- testng.xml

--------------------------------------------------------------------------------
FILE : pom.xml
PATH : slip21/pom.xml
--------------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>practical_Exam</groupId>
  <artifactId>slip21</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.49.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
      </plugin>
      <!-- mvn test runs the suite in testng.xml -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.2.5</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

--------------------------------------------------------------------------------
FILE : LoginPageFactory.java
PATH : slip21/src/test/java/practical_Exam/slips/LoginPageFactory.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

// Page Object using Page Factory
public class LoginPageFactory {

	WebDriver driver;

	@FindBy(id = "user-name")
	WebElement username;

	@FindBy(id = "password")
	WebElement password;

	@FindBy(id = "login-button")
	WebElement loginBtn;

	LoginPageFactory(WebDriver driver) {
		this.driver = driver;
		PageFactory.initElements(driver, this);
	}

	public void login(String user, String pass) {
		username.sendKeys(user);
		password.sendKeys(pass);
		loginBtn.click();
	}
}

--------------------------------------------------------------------------------
FILE : LoginTestFactory.java
PATH : slip21/src/test/java/practical_Exam/slips/LoginTestFactory.java
--------------------------------------------------------------------------------
package practical_Exam.slips;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class LoginTestFactory {

	@Test
	public void loginTest() {
		WebDriver driver = new ChromeDriver();
		try {
			driver.get("https://www.saucedemo.com/");
			LoginPageFactory page = new LoginPageFactory(driver);
			// demo account shown on the saucedemo login page
			page.login("standard_user", "secret_sauce");

			Assert.assertTrue(driver.getCurrentUrl().contains("inventory"), "Login failed");
			System.out.println("Login successful");
		} finally {
			driver.quit();
		}
	}
}

--------------------------------------------------------------------------------
FILE : testng.xml
PATH : slip21/testng.xml
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
    <classes>
      <class name="practical_Exam.slips.LoginTestFactory"/>
    </classes>
  </test>
</suite>
