The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by Saurabh Kamble, 2023-01-02 02:22:12

Selenium Notes

Selenium Notes

package com.sauceDemo.TestClasses;

import java.time.Duration;

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

import com.sauceDemo.POMClasses.HomePOMClass;
import com.sauceDemo.POMClasses.LoginPOMClass;

public class TC003_VerfiyBagProductAddtoCartFunctionality extends TestBaseClass
{

@Test
public void bagProductAddTOCartFunctionality()
{

//homePage
HomePOMClass hp = new HomePOMClass(driver);
hp.clickOnBagButton();

String actaulCount = hp.getTextFromCartButton();

String expectedCount = "1";

if(expectedCount.equals(actaulCount))
{

System.out.println("TC is passed");
}
else
{

System.out.println("TC is failed");
}
}

}
package com.sauceDemo.TestClasses;

import java.time.Duration;

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

import com.sauceDemo.POMClasses.HomePOMClass;
import com.sauceDemo.POMClasses.LoginPOMClass;

public class TC004_VerifyMultipleproductAddtoCartFunctionality extends TestBaseClass
{

@Test
public void mutipleProductAddToCartFunctionality()
{

//homePage
HomePOMClass hp = new HomePOMClass(driver);
hp.clickAllProduct();

String actualCount = hp.getTextFromCartButton();
String expectedCount = "6";

if(expectedCount.equals(actualCount))
{

System.out.println("Tc is passed");
}
else
{

System.out.println("TC is failed");
}
}

}

Practice-1

package testNG;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Practice4
{

@BeforeMethod
public void setUP()
{

System.out.println("broserOpen, UrlOpen, LoginActivity");
}

@Test
public void verifyLogin()
{

System.out.println("LoginFunctionality-TC-PF");
}

@AfterMethod
public void tearDown()
{

System.out.println("logOutActivity, browserClose");
}

}
Practice-2

package testNG;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Practice5
{

@BeforeMethod
public void beforeMethod()
{

System.out.println("Before Method-Pre");
}

@Test
public void test1()
{

System.out.println("Test1-LoginVerify");
}

@Test
public void test2()
{

System.out.println("Test2-LogOutVerify");
}

@Test
public void test3()
{

System.out.println("Test3-SingleVerify");
}

@AfterMethod
public void afterMethod()
{

System.out.println("After Method-post");
}

}

Assignement





Program-1Annotations
package testNG;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Practice6
{

@BeforeMethod
public void beforeMethod()
{

System.out.println("BeforeMethod");
}

@Test
public void Blogin()
{

System.out.println("test1-login");
}

@Test
public void AlogOut()
{

System.out.println("test2-logOut");
}

@AfterMethod
public void afterMethod()
{

System.out.println("AfterMethod");
}

}

Program-2-Annotations

package testNG;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

public class Practice7
{

@BeforeSuite()
public void beforeSuite()
{

System.out.println("Before SUite");
}

@BeforeTest
public void beforeTest()
{

System.out.println("Before Test");
}

@BeforeClass
public void beforeClass()
{

System.out.println("Before Class");
}

@BeforeMethod
public void beforeMethod()
{

System.out.println("BeforeMethod");
}

@Test
public void test1()
{

System.out.println("test1-login");
}

@Test
public void test2()
{

System.out.println("test2-logout");
}

@AfterMethod
public void afterMethod()
{

System.out.println("AfterMethod");
}

@AfterClass
public void afterClass()
{

System.out.println("After Class");
}

@AfterTest
public void afterTest()
{

System.out.println("After Test");
}

@AfterSuite()
public void afterSuite()
{

System.out.println("After SUite");
}

}

package testNG;

import org.testng.annotations.Test;

public class Practice8
{

@Test
public void testA()
{

System.out.println("Test A");
}

@Test(priority=-4)
public void testB()
{

System.out.println("Test B");
}

@Test(priority=3)
public void testC()
{

System.out.println("Test C");
}

@Test(priority=1)
public void testD()
{

System.out.println("Test D");
}

@Test(priority=1)
public void testE()
{

System.out.println("Test E");
}

}

package testNG;

import org.testng.annotations.Test;

public class Practice9
{

@Test(invocationCount=2)
public void testA()

{
System.out.println("Test A");

}

@Test(priority=-1, invocationCount=2)
public void testB()
{

System.out.println("Test B");
}

@Test //by default IC=1

public void testC()

{
System.out.println("Test C");

}
@Test(invocationCount=0)
public void testD()
{

System.out.println("Test D");
}
@Test(invocationCount=-1)
public void testE()
{

System.out.println("Test E");
}
}

package testNG;

import org.testng.annotations.Test;

public class Practice10
{

@Test(invocationCount=0)
public void testA()

{
System.out.println("Test A");

}

@Test(enabled = false,priority=-1,invocationCount=10)
public void testB()
{

System.out.println("Test B");
}

@Test //by default enabled = true

public void testC()

{

System.out.println("Test C");

}

}







package testNG;

import org.testng.annotations.Test;

import graphql.Assert;

public class Practice11
{

@Test(priority=2)
public void loginTest()
{

System.out.println("LoginTest");
Assert.assertTrue(false); //code-TC-fail
}

//run productTest only when the loginTest is passed

@Test(dependsOnMethods= {"loginTest"},priority=1)
public void productAddToCart()
{

System.out.println("Product -add to cart -test");

}
}

package testNG;
import org.testng.annotations.Test;
public class Practice12
{

@Test
public void testA()
{

System.out.println("test A");
}
//assume testB-its taking 10 sec to execute
//i m expecting result in 2 sec
//testB -result in 2 sec--> fine
//else -mark this tc as failed

@Test(timeOut=2000)
public void testB() throws InterruptedException
{

Thread.sleep(10000);
System.out.println("test B");
}

}

Program-1

package testNG;

import org.testng.Assert;
import org.testng.annotations.Test;

public class Assertion2
{

@Test
public void loginTest()
{

String expectdTitle = "Swag Labs";
String actualTitle = "Swag Labs";

//Hard Assertion
// Assert -TestNG
// Static Method -call -help assertion/validation

// Assert.assertEquals(actualTitle, expectdTitle);

//or //failure

message

Assert.assertEquals(actualTitle, expectdTitle,"Titles are

not matching");

//if forcefully wan to fail the test
// Assert.assertTrue(false);

}

}

Program-2
package testNG;

import org.testng.Assert;
import org.testng.annotations.Test;

public class Assertion5
{

@Test
public void loginTest()
{

//title check
String expectdTitle = " Labs";
String actualTitle = "Swag Labs";
Assert.assertEquals(actualTitle, expectdTitle);

//url check
String expectdURL = "https://www.saucedemo.com/";
String actualURL = "https://www.saucedemo.com/";
Assert.assertEquals(actualURL, expectdURL);
}

@Test
public void logOutTest()
{

//pass
Assert.assertTrue(true);
}

}

Program3

package testNG;

import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class Assertion3
{

@Test
public void loginTest()
{

String expectdTitle = " Labs";
String actualTitle = "Swag Labs";

//soft assertion
// SoftAssert-->class--TestNG-->non static

SoftAssert soft = new SoftAssert();

// soft.assertEquals(actualTitle, expectdTitle);

//or
soft.assertEquals(actualTitle, expectdTitle, "Titles are not
matching");

//whenever u will use -soft assert
//compulsory use- one method
//to get the result

soft.assertAll();

}

}

Program4

package testNG;

import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class Assertion6
{

SoftAssert soft = new SoftAssert();

@Test
public void loginTest()
{

//title check
String expectdTitle = " Labs";
String actualTitle = "Swag Labs";
soft.assertEquals(actualTitle, expectdTitle);

//url check
String expectdURL = "https://www.saucedemo.com/";
String actualURL = "https://www.saucedemo.com/";
soft.assertEquals(actualURL, expectdURL);
soft.assertAll();
}

@Test
public void logOutTest()
{

//pass
soft.assertTrue(true);
soft.assertAll();
}

}









<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="SanitySuite">

<test thread-count="4" name="HighPriorityTest">
<classes>
<class name="com.sauceDemo.TestClasses.TC001_VerifyLoginFunctionality"/>
<class name="com.sauceDemo.TestClasses.TC002_VerifyLogOutFunctionality"/>
<class

name="com.sauceDemo.TestClasses.TC003_VerfiyBagProductAddtoCartFunctionality"/>
<class

name="com.sauceDemo.TestClasses.TC004_VerifyMultipleproductAddtoCartFunctionality"/>
</classes>

</test> <!-- Test -->
</suite> <!-- Suite -->

Second suite with HIghP and LowP

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="SanitySuite">

<test thread-count="5" name="HighPriorityTest">
<classes>
<class name="com.sauceDemo.TestClasses.TC001_VerifyLoginFunctionality"/>
<class name="com.sauceDemo.TestClasses.TC002_VerifyLogOutFunctionality"/>
</classes>

</test> <!-- Test -->
<test thread-count="5" name="LowPriorityTest">

<classes>
<class

name="com.sauceDemo.TestClasses.TC003_VerfiyBagProductAddtoCartFunctionality"/>
<class

name="com.sauceDemo.TestClasses.TC004_VerifyMultipleproductAddtoCartFunctionality"/>
</classes>

</test> <!-- Test -->
</suite> <!-- Suite -->

Suite-3-Regression suite

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="RegressionSuite">
<test thread-count="5" name="LowPriorityTest">

<classes>
<class

name="com.sauceDemo.TestClasses.TC003_VerfiyBagProductAddtoCartFunctionality"/>
<class

name="com.sauceDemo.TestClasses.TC004_VerifyMultipleproductAddtoCartFunctionality"/>
</classes>

</test> <!-- Test -->
</suite> <!-- Suite -->



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="SanitySuite">
<test parallel="classes" thread-count="4" name="HighPriorityTest">

<classes>
<class name="com.sauceDemo.TestClasses.TC001_VerifyLoginFunctionality"/>

<class name="com.sauceDemo.TestClasses.TC002_VerifyLogOutFunctionality"/>

<class
name="com.sauceDemo.TestClasses.TC003_VerfiyBagProductAddtoCartFunctionality"/>

<class
name="com.sauceDemo.TestClasses.TC004_VerifyMultipleproductAddtoCartFunctionality"/>

</classes>
</test> <!-- Test -->

</suite> <!-- Suite -->



SuiteLevel Changes

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="SanitySuite">

<test thread-count="4" name="ChromeTest">
<parameter name="browserName" value="chrome"></parameter>

<classes>
<class

name="com.sauceDemo.TestClasses.TC001_VerifyLoginFunctionality"/>
<class

name="com.sauceDemo.TestClasses.TC002_VerifyLogOutFunctionality"/>
<class

name="com.sauceDemo.TestClasses.TC003_VerfiyBagProductAddtoCartFunctionality"
/>

<class
name="com.sauceDemo.TestClasses.TC004_VerifyMultipleproductAddtoCartFunctiona
lity"/>

</classes>

</test> <!-- Test -->
</suite> <!-- Suite -->
BaseClass Changes

package com.sauceDemo.TestClasses;

import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;

import com.sauceDemo.POMClasses.LoginPOMClass;
import com.sauceDemo.UtilityClasses.ScreenshotClass;

public class TestBaseClass
{

WebDriver driver;

@Parameters("browserName")
@BeforeMethod
public void setUP(String browserName)
{
if(browserName.equals("chrome"))
{
System.setProperty("webdriver.chrome.driver",
"D:\\Back-up Data\\Desktop-Backup\\Pavan

Velo\\Automation Batch
Material\\Downloads\\chromedriver_win32\\chromedriver.exe");

driver = new ChromeDriver();
}

else if(browserName.equals("firefox"))
{
System.setProperty("webdriver.gecko.driver",

"D:\\Back-up Data\\Desktop-Backup\\Pavan
Velo\\Automation Batch
Material\\Downloads\\chromedriver_win32\\geckodriver.exe");

driver = new FirefoxDriver();
}
else
{

System.out.println("throw the error");
}

System.out.println("Browser is opened");

driver.manage().window().maximize();
System.out.println("Browser is maximized");

driver.get("https://www.saucedemo.com/");
System.out.println("URL is opened");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

//login
LoginPOMClass lp = new LoginPOMClass(driver);
lp.sendUsername();
lp.sendPassword();
lp.clickOnLoginButton();
}

@AfterMethod
public void tearDown()
{

//logOut

driver.quit();
System.out.println("browser is closed");
}

}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="SanitySuite">

<test thread-count="4" name="ChromeTest">
<parameter name="browserName" value="chrome"></parameter>

<classes>
<class

name="com.sauceDemo.TestClasses.TC001_VerifyLoginFunctionality"/>
<class

name="com.sauceDemo.TestClasses.TC002_VerifyLogOutFunctionality"/>
<class

name="com.sauceDemo.TestClasses.TC003_VerfiyBagProductAddtoCartFunctionality"
/>

<class
name="com.sauceDemo.TestClasses.TC004_VerifyMultipleproductAddtoCartFunctiona
lity"/>

</classes>
</test> <!-- Test -->
<test thread-count="4" name="FirefoxTest">
<parameter name="browserName" value="firefox"></parameter>

<classes>
<class

name="com.sauceDemo.TestClasses.TC001_VerifyLoginFunctionality"/>
<class

name="com.sauceDemo.TestClasses.TC002_VerifyLogOutFunctionality"/>
<class

name="com.sauceDemo.TestClasses.TC003_VerfiyBagProductAddtoCartFunctionality"
/>

<class
name="com.sauceDemo.TestClasses.TC004_VerifyMultipleproductAddtoCartFunctiona
lity"/>

</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->







Pom.xml file

<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>SauceDemo16thApril</groupId>
<artifactId>SauceDemo16thApril</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>

</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>

</dependency>

</dependencies>
</project>










Click to View FlipBook Version