简体   繁体   English

在 pageObject 类中设置 WebDriver,然后在您的测试用例中调用它

[英]Set WebDriver in a pageObject class and then call it in your testcase

Can I write a method for WebDriver in a pageObject class, and then call the method in my testcase class?我可以在 pageObject 类中为 WebDriver 编写一个方法,然后在我的测试用例类中调用该方法吗?

public class setupMethods {

static WebDriver driver;

public static void setup(String browser) throws Exception{
    //Check if parameter passed from TestNG is 'firefox'
    if(browser.equalsIgnoreCase("firefox")){
    //create firefox instance
        System.setProperty("webdriver.gecko.driver", "U:\\path\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
    }
    //Check if parameter passed as 'chrome'
    else if(browser.equalsIgnoreCase("chrome")){
        System.setProperty("webdriver.chrome.driver", "U:\\selenium\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
    }
    //Check if parameter passed as 'Edge'
            else if(browser.equalsIgnoreCase("IE")){
                System.setProperty("webdriver.ie.driver","U:\\path\\IEDriverServer.exe");
                InternetExplorerDriver driver;
                driver = new InternetExplorerDriver();
            }
    else{
        //If no browser passed throw exception
        throw new Exception("Browser is not correct");
    }
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}

} }

This is a method that I would like to call in my test case class, but when I run the test it starts Chrome (or any other browser), but then returns java.lang.NullPointerException when it comes to the line of code where the driver needs to maximize the window driver.manage().window().maximize();这是我想在我的测试用例类中调用的方法,但是当我运行测试时它会启动 Chrome(或任何其他浏览器),但是当涉及到代码行时返回java.lang.NullPointerException驱动程序需要最大化窗口driver.manage().window().maximize();

You're having local variable driver in your method, that's why you're getting this exception.您的方法中有局部变量driver ,这就是您收到此异常的原因。 Remove type declaration for driver in method body.删除方法主体中driver的类型声明。

Here is the Answer to your Question:这是您问题的答案:

Answering straight, Yes , you can write a method for WebDriver in a pageObject class, and then call the method in your testcase class直接回答,的, you can write a method for WebDriver in a pageObject class, and then call the method in your testcase class

You need to consider certain changes in your code as follows:您需要考虑代码中的某些更改,如下所示:

  1. As you need to call the method in my testcase you need to get back the WebDriver instance as a return.由于您需要call the method in my testcase因此您需要取回WebDriver实例作为返回值。 So change the method from public static void setup(String browser) to public static WebDriver setup(String browser)因此,将方法从public static void setup(String browser)更改为public static WebDriver setup(String browser)

  2. As you have already declared the instance of the WebDriver as in static WebDriver driver;因为您已经在static WebDriver driver;中声明了WebDriver的实例; , so you no more need to mention the Class name WebDriver again while initializing the driver as in WebDriver driver = new FirefoxDriver(); ,因此您不再需要在初始化driver时再次提及类名WebDriver ,如WebDriver driver = new FirefoxDriver();

Functionally, this is causing you NullPointerException in your code.从功能上讲,这会导致您的代码出现NullPointerException

  1. There is no best practice but I would prefer to check the passed parameter String browser in if loops only.没有最佳实践,但我更愿意仅在if循环中检查传递的参数String browser

  2. Once you have completed all the actions within this class, return the WebDriver instance.完成此类中的所有操作后,返回WebDriver实例。

  3. Your own code with some simple tweaks will look like:您自己的代码经过一些简单的调整后将如下所示:

     public class setupMethods { static WebDriver driver; public static void setup(String browser) throws Exception { //Check if parameter passed from TestNG is 'firefox' if(browser.equalsIgnoreCase("firefox")) { //create firefox instance System.setProperty("webdriver.gecko.driver", "U:\\path\\geckodriver.exe"); driver = new FirefoxDriver(); } //Check if parameter passed as 'chrome' if(browser.equalsIgnoreCase("chrome")) { System.setProperty("webdriver.chrome.driver", "U:\\selenium\\chromedriver.exe"); driver = new ChromeDriver(); } //Check if parameter passed as 'Edge' if(browser.equalsIgnoreCase("IE")) { System.setProperty("webdriver.ie.driver","U:\\path\\IEDriverServer.exe"); InternetExplorerDriver driver; driver = new InternetExplorerDriver(); } else { //If no browser passed throw exception throw new Exception("Browser is not correct"); } driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); return driver; } }

    Let me know if this Answers your Question.让我知道这是否回答了您的问题。

I have resolved the issue that I had...after using code that @debanjanB suggested, and using constructor for a driver in page object classes by using我已经解决了我遇到的问题......在使用@debanjanB建议的代码并通过使用在页面对象类中使用构造函数作为驱动程序之后

public name_of_pageobject_class(WebDriver driver){ 

    this.driver = setupMethods.driver; 

    }

and calling driver in test cases classes as:并在测试用例类中调用驱动程序为:

WebDriver driver = setupMethods.setup("chrome");

Please correct me if anything might be wrong....but this now works.如果有任何错误,请纠正我......但现在可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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