如何在 selenium webdriver 中调用 java class?

How to call a java class in selenium webdriver?

刚开始学习 selenium-webdriver 并且也在尝试....这是我的问题吗?

下面创建了一个导航到 google 主页的函数

 
package UtilityGoogle;

import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver;

public class HomePage { WebDriver WD=null;

public static void main(WebDriver WD) { // TODO Auto-generated method stub WD = new FirefoxDriver(); WD.navigate().to("https://www.google.co.in"); WD.manage().window().maximize(); return; }

及以下调用主页功能的代码..

package GoogleMain;

import org.openqa.selenium.WebDriver;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
import UtilityGoogle.HomePage;

public class Google_Tc1 {
  private static WebDriver XP = null;@
  Test
  public void Open() {
    HomePage HP = new HomePage();
    String actual = XP.getTitle();
    String expected = "Google";
    AssertJUnit.assertEquals(expected, actual);
  }

}

出现以下错误....请帮我解决这个问题

FAILED: Open
java.lang.NullPointerException
 at GoogleMain.Google_Tc1.Open(Google_Tc1.java:13)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)

您在此处将 WebDriver 设置为 null

private static WebDriver XP = null;

然后在您的 Open() 方法中尝试使用它:

String actual = XP.getTitle();

堆栈跟踪通知您:

at GoogleMain.Google_Tc1.Open(Google_Tc1.java:13)

所以要修复它分配 XP

您的 WebDriver XP 实例变量从未被初始化。 HomePage 中的 main 方法永远不会被调用,即使被调用也没关系,因为它不会操作您尝试在测试中使用的 XP 变量。

我相信这就是你想要达到的目标:

public class HomePage {

    public HomePage(WebDriver driver) {
        driver = new FirefoxDriver();
        driver.navigate().to("https://www.google.co.in");
        driver.manage().window().maximize();
    }

}

和测试class:

public class GoogleTest {

    private static WebDriver driver;

    @Test
    public void Open() {
        HomePage homePage = new HomePage(driver);
        String actual = driver.getTitle();
        String expected = "Google";
        AssertJUnit.assertEquals(expected, actual);
    }

}

现在,当您创建 HomePage 的实例并将驱动程序作为参数传递时,驱动程序已初始化并可以使用。

我更改了变量名,因为您使用的名称不好。你应该阅读 this. I also think there are room for a lot of other improvements. You should probably read about the Page Object Model here and here 并坚持下去。

就像你在首页的main方法中初始化了WD一样class,WD = new FirefoxDriver();
同样在使用前必须先初始化XP。

package GoogleMain;

import org.openqa.selenium.WebDriver;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
import UtilityGoogle.HomePage;

public class Google_Tc1 {
  private static WebDriver XP = new FirefoxDriver();@
  Test
  public void Open() {
    HomePage HP = new HomePage();
    String actual = XP.getTitle();
    String expected = "Google";
    AssertJUnit.assertEquals(expected, actual);
  }

}

public class GoogleTest {

    private static WebDriver driver;

    @Test
    public void Open() {
        HomePage homePage = new HomePage(driver);
        String actual = driver.getTitle();
        String expected = "Google";
        AssertJUnit.assertEquals(expected, actual);
    }

}