简体   繁体   中英

How to access the driver from main method into another method

In below code, I want to access the driver from main method to another method inside same class. To clear the error i put WebDriver driver = new FirefoxDriver(); inside the another method??. Kindly help me out. Thanks in advance for your time

    public class FramesSample {
    public static void main(String[] args) Exception
    {
    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get("url");
    //TODO stuff
    new FramesSample().switchToFrame("parentid", "childid");

    }
    public void switchToFrame(String ParentFrame, String ChildFrame)
    {
    try {
    driver.switchTo().frame(ParentFrame).switchTo().frame(ChildFrame)
    //TODO Stuff
   }
   }

Take it out at class level like this.

Also make it static as you are calling it from static main method.

public class FramesSample {

static WebDriver driver = new FirefoxDriver();     <-------

    public static void main(String[] args) Exception
    {

    driver.manage().window().maximize();
    driver.get("url");
    //TODO stuff
    new FramesSample().switchToFrame("parentid", "childid");

    }
    public void switchToFrame(String ParentFrame, String ChildFrame)
    {
    try {
    driver.switchTo().frame(ParentFrame).switchTo().frame(ChildFrame)
    //TODO Stuff
   }
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM