I have totally four methods in my class
I have created a static WebDriver object
static WebDriver driver;
Method 1: Log-in to site ( Here I initialize the WebDriver driver=new new FirefoxDriver();)
Method 2: Click a link in site ( Using WebDriver driver) On click, the link gets opened in new tab in same browser
Method 3:
Now in Method 3, I switch to new tab and perform some actions with web element in the new tab
Below code is used to switch to new tab
ArrayList<String> tabss = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabss.get(1));
Method 4: Again I want to perform some more action in the new tab
Now I need the same driver instance (tab) used in Method 3 in Method 4.
How do I get that
If I use "driver" in Method 4, it's null.
public class download {
static WebDriver driver;
@Test
public static void login() throws InterruptedException
{
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("__site__");
driver.findElement(By.id("login-email")).sendKeys("__username__");
driver.findElement(By.id("login-password")).sendKeys("__password__");
driver.findElement(By.id("login-submit")).click();
Thread.sleep(3000);
}
@Test
public static void navigatetolearningpage() throws InterruptedException
{
driver.findElement(By.xpath("//div[@class='relative ember-view']")).click();
Thread.sleep(3000);
}
@Test
public static void search() throws InterruptedException, AWTException
{
ArrayList<String> tabss = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabss.get(1));
driver.findElement(By.xpath("//input[@type='text']")).sendKeys("__searchkeyword__");
Thread.sleep(3000);
driver.findElement(By.xpath("//input[@type='text']")).sendKeys(Keys.RETURN);
Thread.sleep(3000);
driver.findElement(By.xpath("//div[@class='search-facet__label']")).click();
}
@Test
public static void course_list() throws InterruptedException
{
//This driver will print as NULL
System.out.println("last method:"+driver);
}
}