Thursday, 24 November 2022

TestNG Project add_to_cart.java file test case code

package ExtentReports;

import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import org.openqa.selenium.By;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Add_To_Cart extends Load_Test{

@BeforeTest
public void before_add_to_cart(){

}

@Test
public void test_Add_to_cart(){
ExtentTest test = extent.createTest("Add To Cart Button Test ");
try{
driver.findElement(By.id("add-to-cart-sauce-labs-backpack")).click();
String text = driver.findElement(By.id("add-to-cart-sauce-labs-backpack")).getText();
if(text == "REMOVE"){
test.log(Status.INFO,"Add to cart button test: Pass");
extent.flush();
}
else{
test.log(Status.INFO,"Add to cart button test: Fail");
extent.flush();
}
}
catch (Throwable t){
System.out.println("Add to cart Exception: "+t);
}
}

@AfterTest
public void after_add_to_cart(){

}

}

--
With Warm REGARDS,

En. S. WANKHEDE

TestNG project Check_Drop_Down.java test case

package ExtentReports;

import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Check_Drop_Down extends Load_Test{

@BeforeTest
public void before_drop_down(){

}

@Test
public void Test(){
ExtentTest drop_test = extent.createTest("Drop Down Test");
try{
WebElement element = driver.findElement(By.xpath("/html/body/div/div/div/div[1]/div[2]/div[2]/span/select"));
Select select = new Select(element);
select.selectByIndex(3);
String option = element.getText();

drop_test.log(Status.INFO,"Selected drop down Option: "+option);
extent.flush();
}
catch (Throwable t){
//drop_test.log(Status.INFO,"Exception: "+t);
//extent.flush();
System.out.println("Drop Down Exception: "+t);
}
}

@AfterTest
public void after_drop_down(){

}
}

--
With Warm REGARDS,

En. S. WANKHEDE

TestNG Project Check_Logging_in.java test case

package ExtentReports;

import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import org.openqa.selenium.By;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Check_Logging_In extends Load_Test{

@BeforeTest
public void before_login(){

}


@Test
public void Login(){
ExtentTest login_test = extent.createTest("Login with USERNAME & PASSWORD Test");
try{
driver.findElement(By.id("user-name")).sendKeys("standard_user");
driver.findElement(By.id("password")).sendKeys("secret_sauce");
driver.findElement(By.id("login-button")).click();
String username = driver.findElement(By.id("user-name")).getText();
login_test.log(Status.INFO, "Username: "+username);
login_test.log(Status.INFO,"Logging in: Pass");
extent.flush();
}
catch (Throwable t){
//login_test.log(Status.INFO, "Exception: "+t);
//System.out.println("Exception: "+t);
System.out.println("Logging in: "+t);
}
}

@AfterTest
public void after_login(){

}
}

--
With Warm REGARDS,

En. S. WANKHEDE

TestNG Project check_menu_button.java file

package ExtentReports;

import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.util.List;

public class Check_Menu_Button extends Load_Test{

@BeforeTest
public void before_menu(){

}

@Test
public void test_Menu_Button(){
ExtentTest menu_test = extent.createTest("Menu_Button Test");

try{
driver.findElement(By.id("react-burger-menu-btn")).click();
List<WebElement> listelement = driver.findElements(By.className("bm-item-list"));
for (int i=0; i<listelement.size();i++){
menu_test.log(Status.INFO,"Menu: "+listelement.get(i).getText());
extent.flush();
}
menu_test.log(Status.INFO,"Menu List Check");
extent.flush();
}
catch (Throwable t){
System.out.println("Menu bar Exceptions: "+t);

}
}

@AfterTest
public void after_menu(){

}


}

--
With Warm REGARDS,

En. S. WANKHEDE

TestNG Project check_page_info..java file selenium test case

package ExtentReports;

import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import org.openqa.selenium.By;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Check_Page_Info extends Load_Test{

@BeforeTest
public void before_Page_Info(){

}

@Test
public void Test_Page_Info(){
ExtentTest page_test = extent.createTest("Page Test");

try{
String page_title = driver.findElement(By.className("title")).getText();
if(page_title == "PRODUCTS"){
page_test.log(Status.INFO,page_title+" - Page loaded: Pass");
page_test.log(Status.INFO, new Throwable());
extent.flush();
}
else{
page_test.log(Status.INFO,page_title+" - Page Not Loaded: Fail");
page_test.log(Status.INFO, new Throwable());
extent.flush();
}

}
catch (Throwable t){
page_test.log(Status.INFO,"Exception Generated: "+t);
System.out.println("Exception: "+t);
extent.flush();
}
}
}

--
With Warm REGARDS,

En. S. WANKHEDE

TestNG Project check_page_info.java file

package ExtentReports;

import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import org.openqa.selenium.By;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Check_Password_Box extends Load_Test{

@BeforeTest
public void before_pass(){

}
@Test
public void Test_pass(){
ExtentTest test5 = extent.createTest("Check Password TextBox");
if(driver.findElement(By.id("password")).isDisplayed()){
if (driver.findElement(By.id("password")).isEnabled()){
test5.log(Status.INFO,"Check password TextBox is Displayed: Pass");
test5.log(Status.INFO,"Check Password textBox is Enabled: Pass");
extent.flush();
}
else {
test5.log(Status.INFO, " Check password TextBox is Displayed: Pass");
test5.log(Status.INFO, " Check password TextBox is Enabled: Failed");
extent.flush();
}
}
else{
test5.log(Status.INFO, " Check password TextBox is Displayed: Failed");
test5.log(Status.INFO, "Check password TextBox is Enabled: Failed");
extent.flush();
}
}

@AfterTest
public void after_pass(){

}
}

--
With Warm REGARDS,

En. S. WANKHEDE

TestNG project check_user_name.java file

package ExtentReports;

import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import org.openqa.selenium.By;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Check_User_Name extends Load_Test {

@BeforeTest
public void Before_User_name(){

}

@Test
public void Test_User_name(){
ExtentTest test4 = extent.createTest("Check User Name Text Box");
if(driver.findElement(By.id("user-name")).isDisplayed()){
if(driver.findElement(By.id("user-name")).isEnabled()){
test4.log(Status.INFO,"User Name TextBox is Displayed: Pass");
test4.log(Status.INFO, "User Name TextBox is Enabled: Pass");


extent.flush();

}
else{
test4.log(Status.INFO,"User Name TextBox is Displayed: Pass");
test4.log(Status.INFO,"User Name TextBox is Enabled: Failed");
extent.flush();
}
}
else{
test4.log(Status.INFO, "User Name TextBox is Displayed: Failed");
test4.log(Status.INFO, "User Name TextBox is Enabled: Failed");
extent.flush();
}
}

@AfterTest
public void After_user_name(){

}

}

--
With Warm REGARDS,

En. S. WANKHEDE

TestNG Project Count_card.java file selenium project

package ExtentReports;

import com.aventstack.extentreports.ExtentTest;

import com.aventstack.extentreports.Status;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.awt.*;
import java.util.List;

public class Count_Cards extends Load_Test{

@BeforeTest
public void before_count_cards(){

}

@Test
public void count_cards(){
ExtentTest card_test = extent.createTest("Card Count Test");
try{
List<WebElement> elementList = driver.findElements(By.className("inventory_item"));
for (int i = 0; i<elementList.size();i++) {
System.out.println(elementList.get(i));
card_test.log(Status.INFO,"Option: "+elementList.get(i).getText());
extent.flush();
}
card_test.log(Status.PASS,"Cards Count: "+elementList.size());
extent.flush();
}
catch (Throwable t){
//card_test.log(Status.INFO,"Exception: "+t);
//extent.flush();
System.out.println("Card Count Exception: "+t);
}
}

@AfterTest
public void after_count_card(){

}
}

--
With Warm REGARDS,

En. S. WANKHEDE

TestNG project reference file which is inherited by each test case file


TestNG project 

package ExtentReports;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Reporter;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Load_Test {

public static WebDriver driver;
public static ExtentReports extent;
public static ExtentSparkReporter spark;



@BeforeTest
public void initialize(){
extent = new ExtentReports();
spark = new ExtentSparkReporter("target/Spark/Spark.html");
spark.config().setReportName("Sample_Report_1");
spark.config().setDocumentTitle("Insurance Project Us Client ");
spark.config().setTheme(Theme.DARK);
spark.config().setEncoding("utf-8");
extent.attachReporter(spark);
}
@Test
public void load(){


}

@AfterTest
public void end(){
extent.flush();
}

}



--
With Warm REGARDS,

En. S. WANKHEDE

TestNG Project take screenschot test_case selenium project

package ExtentReports;

import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.annotations.Test;

import java.io.File;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


public class Take_Screenschot extends Load_Test {


LocalDateTime dt = LocalDateTime.now();
DateTimeFormatter dt_format = DateTimeFormatter.ofPattern("dd-MM-yy HH:mm:ss");
String time_now = dt.format(dt_format);



@Test
public void test_screenschot(){
System.out.println("Time Now: "+time_now);
ExtentTest test = extent.createTest("Takeing Schreenschot.");

try{
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String path = "target/Spark/homePageScreenshot.png"+time_now;
FileUtils.copyFile(screenshot, new File("target/Spark/homePageScreenshot.png"));
test.log(Status.INFO, "target/Spark/homePageScreenshot.png");
extent.flush();
}
catch (Throwable t){
System.out.println("Exceptions: "+t);
}
}

}

--
With Warm REGARDS,

En. S. WANKHEDE

Charactor_At_Position

public class Char_At_Position { public static void main(String[] args) { String str = "Wankhede"; String rev=""...