Tuesday, September 1, 2020

Cross Browser Testing in TestNG| Parallel Testing in multiple browser using TestNG in selenium | @Parameter annotation in TestNG






In real time scenario we have to run our selenium test script in multiple browser simultaneously.
TestNG makes is possible to achieve that using @Parameter annotation.


TestNG allows the user to pass values to test methods as arguments by using parameter annotations through testng. xml file. Some times it may be required for us to pass values to test methods during run time. ... The @Parameters annotation can be placed on any method that has a @Test, @Before/After or @Factory annotation. Here i will show an example that how can we use this annotation and run our test cases in parallel .

package Baseclass;

import java.io.IOException;
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.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;

import io.github.bonigarcia.wdm.WebDriverManager;

public class Baseclass {

	public static WebDriver driver;
	public static pageobjects pageobjects;
	
@Test
@Parameters(value="browsername")
public static void setup(String browsername)
{
	if(browsername.equalsIgnoreCase("chrome"))
	{
	WebDriverManager.chromedriver().setup();
	driver=new ChromeDriver();
	}
	
	if(browsername.equalsIgnoreCase("ff"))
	{
	WebDriverManager.firefoxdriver().setup();
	driver=new FirefoxDriver();
	}
	
	driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
	driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
	driver.get("https://www.goibibo.com/");
	driver.manage().window().maximize();
	String title=driver.getTitle();
	
	System.out.println("page title is ---->>"+title);
	pageobjects=new pageobjects(driver);	
}

This is my one class which will run my test based on browsername parameter which it get from the TestNG file.

Now, just create a TestNG file of this project by Right click on project name > TestNG>convert to TestNG





Now, add  this code to the TestNG.xml file.

		<parameter name="browsername" value="chrome">
		</parameter>

This will take browsername=chrome and run this test on chrome. Now, to run this test on multiple browser we need to create other test tag for other browser. we need to create N of test tag for N browser. at the end our TestNG.xml file will look like this


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

<suite name="Suite" parallel="tests" thread-count="2">


	<test name="testonchrome">

		<parameter name="browsername" value="chrome">
		</parameter>

		<classes>
			<class name="Baseclass.Hotelsearch" />
		</classes>
	</test>
	
	<test name="testonff">

		<parameter name="browsername" value="ff">
		</parameter>

		<classes>
			<class name="Baseclass.Hotelsearch" />
		</classes>
	</test>
</suite> 
Now, run this TestNG.xml file you will see the result it will run testcase in chrome and firefox parallely.

Sunday, June 14, 2020

What is WebDriverEventListner in selenium? | TestNG Listner example | How to generate Logs for the action in selenium script?

Lister concept in TestNG is very easy to understand and anyone can learn it doesn't required that you should be expert in it. in Simple words  TestNG listners as the name say it 'Listen' something. it helps you to keep track of execution of script , It helps in generating logs and many more things.





 
There are 2 Type of Listener in TestNG : 
1) TestNG listner (iTestResult,iRetryAnalyser,etc)
2) WebDriver listner(WebDriverEventListner)

Here, i will show you example of WebDriverEventListner.


To work with WebDriverEventListner. we need 2 things :-

1) class A with implement abstract method of WebDriverEventListener

2) class B which will register the class A's action 


Lets see how we actually work.

This is my class A which implements the abstract of WebDriverEventListner

package com.Listners;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;

import com.baseclass.Baseclass;
import com.pageobject.Pageobjects;

public class Listner extends Baseclass implements WebDriverEventListener {

	@Override
	public void afterAlertAccept(WebDriver arg0) {

		System.out.println("alert got----->" +arg0.switchTo().alert());
	}

	@Override
	public void afterAlertDismiss(WebDriver arg0) {
		
	}

	@Override
	public void afterChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
		
	}

	@Override
	public void afterClickOn(WebElement arg0, WebDriver arg1) {
		
		
	System.out.println("clicked on---->"+arg0.getText().toString());
		
	}

	@Override
	public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
		
	}

	@Override
	public <X> void afterGetScreenshotAs(OutputType<X> arg0, X arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterGetText(WebElement arg0, WebDriver arg1, String arg2) {
		
	}

	@Override
	public void afterNavigateBack(WebDriver arg0) {
		
	}

	@Override
	public void afterNavigateForward(WebDriver arg0) {
		
	}

	@Override
	public void afterNavigateRefresh(WebDriver arg0) {
		
	}

	@Override
	public void afterNavigateTo(String arg0, WebDriver arg1) {
      
		System.out.println("navigated to--->"+arg1.getCurrentUrl());
	}

	@Override
	public void afterScript(String arg0, WebDriver arg1) {
		
	}

	@Override
	public void afterSwitchToWindow(String arg0, WebDriver arg1) {

		System.out.println("switch to the window--->"+driver.switchTo().window(arg0));
	}

	@Override
	public void beforeAlertAccept(WebDriver arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beforeAlertDismiss(WebDriver arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beforeChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beforeClickOn(WebElement arg0, WebDriver arg1) {

		System.out.println("Trying to click on---->"+arg0.getText().toString());
	}

	@Override
	public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public <X> void beforeGetScreenshotAs(OutputType<X> arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beforeGetText(WebElement arg0, WebDriver arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beforeNavigateBack(WebDriver arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beforeNavigateForward(WebDriver arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beforeNavigateRefresh(WebDriver arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beforeNavigateTo(String arg0, WebDriver arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beforeScript(String arg0, WebDriver arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beforeSwitchToWindow(String arg0, WebDriver arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onException(Throwable arg0, WebDriver arg1) {
		// TODO Auto-generated method stub
		
	}

}



As you can see there are many methods available but for the practice purpose i have used only few
methods.



Now, we just need  class which will use this class as registering actions.



package com.baseclass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;

import com.Listners.Listner;

import io.github.bonigarcia.wdm.WebDriverManager;

public class Baseclass {

	public static WebDriver driver;
	
	public static EventFiringWebDriver e_driver;
	
		public static void browsersetup()
	{
		WebDriverManager.chromedriver().setup();
		driver=new ChromeDriver();
		e_driver=new EventFiringWebDriver(driver);
		Listner listner=new Listner();
		e_driver.register(listner);
		driver=e_driver;
		driver.get("https://wordpress.com/log-in");	
		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
		
	}
}





Now we are done with the setup and can use this class for capturing the events So, to use this i am showing you an example of wordress site. i will write script for creating one post and than delete it. and we will see how this listner will work.





package com.test;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import com.baseclass.Baseclass;
import com.pageobject.Pageobjects;

public class Login extends Baseclass {
	
	public static Pageobjects  pom;

	
	@BeforeSuite
	public void setupm()
	{
		browsersetup();
		pom=new Pageobjects(driver);	
	}
	
	@Test
	public static void login(Pageobjects pom)
	{
       pom.username.sendKeys("xxxx@gmail.com");	
       pom.continue_button.click();
       pom.password.sendKeys("xxxxx");
       pom.signin_button.click();
       
       
       
	}



}


Now, if we run this script we will see whatever action is performing in screen will be shown in console like this.






Sunday, May 31, 2020

How to verify Success message or Warning message or Tooltip in selenium?

Common scenario we get while automating something is to check assertion after performing any action ( create , update , delete )

In this case we usually get some messages on screen that is getting disappear after sometime. and problem we face that we cant inspect them because it gets disappear.

In order to verify those kind of messages i got something which i am sharing here which might help you :)

Here, in this example i am showing example of wordpress while deleting the post we are getting one success message as 'Post is deleted successfully' which gets disappear when we try to inspect it so how can we inspect it like this






In order to inspect it

Step no 1:- After performing an operation go to the Inspect element > Sources TAB and click on Pause button below like this





it will stop the script execution.


Step no 2:- Now, go that message and try to inspect it





Step no 4:- Get the X-path of that element 





Step no 5:- Get the X-path of that element and use .getText() method to fetch value into it. and use it for assertion.


@FindBy(how=How.XPATH,using="//span[@class=\"notice__text\"]")
@CacheLookup 
public WebElement deletepost_message;


String delete_message=pom.deletepost_message.getText();
System.out.println("post is deleted and success message displayed as    "+ delete_message);

Sunday, March 29, 2020

Print binary tree pattern in Java

To print a binary pattern in java is simple thing. its same like printing stars the only logic changes is here we have to print 1 or 0 instead of star. and thats also very easy. if we observe when (i+j%2==0) that time we have to print 1 else 0.
public class pattern2 {
 public static void main(String[] args) {



  for(int i=0;i<=4;i++)
  {
   for(int j=0;j<=i;j++)
   {
    if((i+j)%2==0)
    {
     System.out.print(1);
    }
    else
    {
     System.out.print(0);
    }
   }
   System.out.println();
  }
 }
}
Output:-
1
01
101
0101
10101

Reverse Pyramid star patterns in java

To print  reverse pyramid pattern  in java is same like we do print pyramid in increment size. the only think change here is , here we run i ( row) loop in reverse manner.
public class Pattern1 {

 public static void main(String[] args) {
  
  for(int i=4;i>=0;i--)
  {
   for(int j=0;j<=i;j++)
   {
    System.out.print("*");
   }
   System.out.println();
  }
 }
}
OUTPUT:-
 *****
 ****
 ***
 **
 *


To check weather given number is prime or not in Java.

To check weather number is prime or not is simple. we need to take a number as a input and than we need run a for loop till the number-1 time and module each of the for loop variable with the number given. so by that if we get module = 0 , which means number can be devide and hence number is ot prime. 
import java.util.Scanner;

public class Primenumbers {

 public static void main(String[] args) {
  Scanner sc=new Scanner(System.in);
  System.out.println("enter the number->");
  int num=sc.nextInt();
  int flag=0;


  if(num==0||num==1)
  {
   System.out.println("please enter the valid number");
  }
  else
   for(int i=2;i<num/2;i++)
   {
    if(num%1==0)
    {
     System.out.println("not prime");
     flag=1;
     break;
    }
   }

  if(flag==0)
  {
   System.out.println("prime");
  }


 }

}

To Print occurence of a character in given String in java.

To print Occurrence of  a given character is a simple thing. We need compare each character of a given string with the character we need to compare. and if both matches than we have to increase the counter by 1.
import java.util.Scanner;

public class Accurence_char {
 
 public static void main(String[] args) {
  Scanner sc=new Scanner(System.in);
  System.out.print("enter the string:--");
  String str=sc.next();
  char check='o';
  int count=0;
  
 for(int i=0;i<str.length();i++)
 {

  if(str.charAt(i)==check)
  {
   count++;
  }
  
  
 }
 
 System.out.println("occurence of "+check+" in given string is:"+count);
 
 }
 

}

Find a factorial of a given number | Basic java programs for interview for Test automation engineer

To find a factorial of a given number is very simple. For exp if we have to find factorial of 5 than we have to (5*4*3*2*1) here we are doing multipication of a number and print sum of it.

import java.util.Scanner;

public class factorial {

 public static void main(String[] args) {


  Scanner sc= new Scanner(System.in);
  System.out.println("enter the number: ");

  int num=sc.nextInt();

  int sum=1;

  for(int i=num;i>=1;i--)

  {
   sum=sum*i;
   if(i==1)
   {
    System.out.println("factorial of"+" "+num+" "+ "is-->" +" "+sum);
   }

  }
 }


}

Print Fibonacci series of a number in java | Basic java program for Test automation engineer interview

Fibonacci series is 1,1,2,3,5,8,13,21,34,55,etc..

Here simple logic is Take the first number a=1,b=1, and do sum of that c=a+b.
and after that replace the value of a and b.

Lets look at the code.
import java.util.Scanner;

public class Fibonaci {
 /* 1 1 2 3 5 8 13 21 34
  * 
  * 
  * 
  */

 public static void main(String[] args) {

  int a=1;
  int b=1;
  int c = 0;
  Scanner sc=new Scanner(System.in);
  System.out.println("enter the number till fibonaci series you want to print: ");
  int num=sc.nextInt();
  System.out.print(a+" "+b);

  while(c<num)
  {
   c=a+b;

   System.out.print(" "+c+" ");

   a=b;
   b=c;


  }

 }
}

To check weather give string is palindrom or not | Basic java programs for Interview for Test automation engineer

1) To check weather a given string is palindrome or not in java.

Palindrome string a string which is same while reading from forward to backword or backword to forword (Ex. madam,lo,pop,rubber)

-To do that that there is very simple logic in java. 
1)We need to take a length of the string
2)Reverse a string
3)compare both the string



import java.util.Scanner;

public class Reversestring {

 public static void main(String[] args) {

  Scanner sc= new Scanner(System.in);
  System.out.println("enter the string: "); 
  String str=sc.next(); 
  int length=str.length();

  String s2="";

  for(int i=length-1;i>=0;i--)

   s2=s2+str.charAt(i);

  if(s2.equalsIgnoreCase(str))
  {
   System.out.println("palindrome");

  }
  else {
   System.out.println("not palindrom");
  }

 }

}