Selenium WebDriver Hands-on Solutions | TCS Fresco Play
Selenium WebDriver Fresco Play Hands-on Solutions
Course Path: DevOps/TESTING/Selenium WebDriver
Disclaimer: The primary purpose of providing this solution is to assist and support anyone who are unable to complete these courses due to a technical issue or a lack of expertise. This website's information or data are solely for the purpose of knowledge and education.
Make an effort to understand these solutions and apply them to your Hands-On difficulties. (It is not advisable that copy and paste these solutions).
All Question of the MCQs Present Below for Ease Use Ctrl + F with the question name to find the Question. All the Best!
Selenium WebDriver MCQ Solution
1.TestNG Annotation Hands-On
File Name - TestNG_Annotations.java
package Testng;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
public class TestNG_Annotations {
// Write your code here
@AfterSuite
public void afterSuite(){
System.out.println("After Suite has been executed as no.9");
}
@BeforeClass
public void beforeClass(){
System.out.println("Before Class has been executed as no.3");
}
@BeforeTest
public void beforeTest(){
System.out.println("Before Test has been executed as no.2");
}
@BeforeMethod
public void beforeMethod(){
System.out.println("Before Method has been executed as no.4");
}
@Test
public void GetpageTitle(){
System.out.println("Test has been executed as no.5");
}
@AfterMethod
public void afterMethod(){
System.out.println("After Method has been executed as no.6");
}
@AfterTest
public void afterTest(){
System.out.println("After Test has been executed as no.8");
}
@BeforeSuite
public void beforeSuite(){
System.out.println("Before Suite has been executed as no.1");
}
@AfterClass
public void afterClass(){
System.out.println("After Class has been executed as no.7");
}
}
Selenium WebDriver MCQ Solution
2.Hands-On
Welcome to Launch Browser with TestNG
File Name - LaunchBrowser_TestNG
package launchBrowserTestNG;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
public class LaunchBrowser_TestNG {
@Test
public void LaunchBrowser() throws InterruptedException{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "/projects/challenge/phantomjs-2.1.1-linux-x86_64/bin/phantomjs");
WebDriver driver = new PhantomJSDriver(caps);
System.out.println("PhantomJS Headless Driver launched");
// Write your script here
driver.get("https:/google.com");
Thread.sleep(5000);
System.out.println("Launch Browser is successful");
System.out.println("Page Title : " + driver.getTitle());
//Searching for "Fresco Play" in Google search
driver.findElement(By.xpath("//input[@name='q']")).sendKeys("Fresco Play");
driver.findElement(By.xpath("//input[@name='q']")).sendKeys(Keys.ENTER);
Thread.sleep(5000);
System.out.println("Page Title : " + driver.getTitle());
}
}
Selenium WebDriver MCQ Solution
File Name - 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>com</groupId>
<artifactId>launchBrowserTestNG</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>launchBrowserTestNG</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<suiteXmlFile>launchBrowserTestNG/testng.xml</suiteXmlFile>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.11.0</version>
</dependency>
<!-- Add you Testng dependency here...!! -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.detro.ghostdriver</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
File Name - testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Test">
<classes>
<class name="launchBrowserTestNG.LaunchBrowser_TestNG"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Selenium WebDriver MCQ Solution
3.Cross Browser - Hands-On
Welcome to Cross Browser Testing Selenium
File Name - googleLaunch.java
package crossBrowsers;
import org.testng.annotations.Test;
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.chrome.ChromeOptions;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import io.github.bonigarcia.wdm.ChromeDriverManager;
public class googleLaunch {
public WebDriver driver;
@BeforeTest
@Parameters("browser")
public void SelectBrowser(String browser)
{
// Write your script here
if(browser.equalsIgnoreCase("ChromeHeadless"))
{
ChromeDriverManager.chromedriver().setup();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
System.out.println("Chrome Headless Driver launched");
}
else if(browser.equalsIgnoreCase("PhantomJS"))
{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "/projects/challenge/phantomjs-2.1.1-linux-x86_64/bin/phantomjs");
driver = new PhantomJSDriver(caps);
System.out.println("PhantomJS Headless Driver launched");
}
driver.manage().window().maximize();
driver.get("https://www.google.com");
}
@Test
public void GoogleSreach() throws InterruptedException{
// Searching for "Fresco Play" in Google search.
// Find the elementlocators for the below actions.
// 1) Find locator for Google Search Box and sendkeys as Fresco Play as below.
driver.findElement(By.xpath("//input[@name='q']")).sendKeys("Fresco Play");
// 2) Find locator for Google Search Box and sendkeys as Enter as below.
driver.findElement(By.xpath("//input[@name='q']")).sendKeys(Keys.ENTER);
Thread.sleep(6000);
System.out.println("Page Title : " + driver.getTitle());
}
@AfterTest
public void BroswerQuit(){
driver.quit();
}
}
Selenium WebDriver MCQ Solution
4.Hands-On: POM Normal Approach
Welcome to POM Normal Approach
File Name - LoginPage.java
package Pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPage {
WebDriver driver;
// write your script here.
By LoginTab=By.xpath("//*[@id='pt-login']/a");
By username=By.xpath("//*[@id='wpName1']");
By password=By.xpath("//*[@id='wpPassword1']");
By logIn=By.xpath("//*[@id='wpLoginAttempt']");
public LoginPage(WebDriver driver){
this.driver=driver;
}
public void clickLoginTab(){
driver.findElement(LoginTab).click();
}
public void typeUserName(){
driver.findElement(username).sendKeys("");
}
public void typePassword(){
driver.findElement(password).sendKeys("");
}
public void clickLoginButton(){
driver.findElement(logIn).click();
}
}
Selenium WebDriver MCQ Solution
5.Hands-On: Page Factory Method
Welcome to Page Factory Method
File Name - verfiyLogin_PageFactory.java
package TestCases;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import Pages.PageFactoryLogin;
import utility.BrowserFactory;
public class verfiyLogin_PageFactory {
WebDriver driver;
@Test
public void checkValidUser(){
WebDriver driver = BrowserFactory.startBrowser("PhantomJS", "https://en.wikipedia.org/wiki/Main_Page");
PageFactoryLogin login_page = PageFactory.initElements(driver, PageFactoryLogin.class);
// Write your username and password in below line
login_page.Login("Abarna6898","abcdefg");
System.out.println("Page Title : " + driver.getTitle());
driver.quit();
System.out.println("Browser Quit is successful");
}
}
File Name - BrowserFactory.java
package utility;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
public class BrowserFactory {
static WebDriver driver;
public static WebDriver startBrowser(String browserName, String url ){
if (browserName.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "/root/selenium/chromedriver");
driver=new ChromeDriver();
}
else if (browserName.equalsIgnoreCase("HtmlUnitDriver")){
driver = new HtmlUnitDriver(true);
}
else if (browserName.equalsIgnoreCase("PhantomJS"))
{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
String [] phantomJsArgs = {"--ignore-ssl-errors=yes"};
caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomJsArgs);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "/projects/challenge/phantomjs-2.1.1-linux-x86_64/bin/phantomjs");
driver = new PhantomJSDriver(caps);
System.out.println("PhantomJS Headless Driver launched");
}
// Write your script here
driver.manage().window().maximize();
driver.get(url);
return driver;
}
}
File Name - PageFactoryLogin.java
package Pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
public class PageFactoryLogin {
WebDriver driver;
public PageFactoryLogin(WebDriver localdriver){
this.driver=localdriver;
}
// Write your script here
@FindBy(xpath = "//*[@id='pt-login']/a")WebElement loginTab;
@FindBy(id = "wpName1")WebElement username;
@FindBy(how = How.ID,using = "wpPassword1")WebElement password;
@FindBy(how = How.XPATH,using = "//*[@id='wpLoginAttempt']")WebElement logIn;
public void Login(String uid, String pass){
loginTab.click();
username.sendKeys(uid);
password.sendKeys(pass);
logIn.click();
}
}
>
Selenium WebDriver MCQ Solution
*******************************************************
If you want answers for any of the fresco play courses, please ask in the comment section.
Thanks!
3 comments