MainMenu

Home Java Overview Maven Tutorials

Sunday, 22 April 2018

Create WebDriver instance for multiple class in different ways


Hello friends,

In this article we will learn how to create WebDriver Instance multiple classes:

So, we will create a WebDriver instance in one class & call it in multiple class
we have achieve this by 2 ways
1).By using extends
2).By using Singleton class

To know about singleton class Click Here

For Video :- Click Here



Method :1By extends Keyword

Step :1Create a class & initialize your driver

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class MainDriver
{
public static WebDriver driver;
public static void createInstance()
{
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\Complete selenium\\ChromeDriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
}
}


Step :2Create an another class & extend above driver class

public class Testcase1 extends MainDriver
{
public static void url()
{
MainDriver.createInstance();
driver.navigate().to("http://www.way2testing.com");
}
}


Step :3Create an another class & extend above driver class

public class TestCase2 extends MainDriver
{
public static void getTitle()
{
System.out.println(driver.getTitle());
}
}


Step :4Create an another class & extend above driver class

import org.testng.annotations.Test;

public class Excution
{
@Test(priority = 0)
public void getUrl()
{
Testcase1.url();
}
@Test(priority = 1)
public void getTitle()
{
TestCase2.getTitle();
}
}


If you will run this Exeution class , you can see that with one WebDriver Instance(driver), you are able to perform your all task.

Method :2By Singleton Class

Step :1Create a Singleton class & initialize your driver

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SingletonDriver
{
public static WebDriver driver = null;
public static void Initiallize()
{
if(driver == null)
{
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\Complete selenium\\ChromeDriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
}
}
public static void close()
{
driver.close();
}
public static void quit()
{
driver.quit();
}
}



Step :2Create a class for your test cases & execute singleclass method

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Testcases
{
@BeforeTest
public void initializeDriver()
{
SingletonDriver.Initiallize();
}
@Test(priority = 0)
public void openurl()
{
SingletonDriver.driver.navigate().to("http://www.way2testing.com");
}
@Test(priority = 1)
public void getTitle()
{
System.out.println(SingletonDriver.driver.getTitle());
}
}



So, by using above methods, you can create a WebDriver Instanse(driver) and can use it in your multiple classes.
Thanks :)





Swap Two variable in Java


Hello friends,

In this article we will discuss about swap variable without using third variable:

For Video :- Click Here



public class Swap
{
int x = 10;
int y = 15;
public static void main(String args[])
{
Swap object = new Swap();
object.g();
}
public void g()
{
x = x+y;
y = x-y;
x = x-y;
System.out.println("After Swap Value of y : " + y);
System.out.println("After Swap Value of x : " + x);
}
}


output :
After Swap Value of y : 10
After Swap Value of x : 15

Code Explanation : Suppose we have two variable x, y and they have value 10, 15 respectively and we want to swap these two variables value.
HOW IT WOULD BE DONE ?
1). First , i will join these two variables value & store it in variable x.
Now the value of x is 25.
2). Now, we will minus the value of y(15) from this x(25).
we will get 10 & will store in variable y
3). Now the value on y is 10 & value of x is 25.
again we will minus this y value from & store it in variable x so value of variable of x will become 15.

Congratulation You have swapped the value of two variable without using third variable. :)





1. Write a program to swap two string without using any third string/variable

Answer :- First add both the string and store in a existing variable then use String method Substring to get the string


Below is program


public class PracticeA {

public static void main(String args[]) {

String x = "chandan";
String y = "singh";

System.out.println("Value of x: " +x + " , " + "Value of y :" + y + " ," +"before swapping");
x = x+y;

y = x.substring(0, x.length()-y.length());

x = x.substring(y.length());
System.out.println("Value of x: " +x + " , " + "Value of y :" + y + " ," +"after swapping");
}
}



Output :- Value of x: chandan, Value of y :singh ,before swapping Value of x: singh, Value of y :chandan ,after swapping

---------------------------------------------------------------------------




Tags :

Swap two variable without using third variable in Java

how two swap two variable in java without using third variable