[英]How to call a java class in selenium webdriver?
just started learning selenium-webdriver and trying too....soo here is my dout? 刚开始学习selenium-webdriver并尝试.... soo这是我的道歉?
below created a function navigate to google home page 下面创建了一个功能导航到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; }
and below code calling Homepage function.. 和下面的代码调用Homepage函数。
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); } }
Getting below error .... please do help me to fix this 遇到错误....请帮助我解决此问题
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)
You set the WebDriver to null
here: 您可以在此处将WebDriver设置为
null
:
private static WebDriver XP = null;
Then in your Open()
method try to use it: 然后在您的
Open()
方法中尝试使用它:
String actual = XP.getTitle();
The stack trace informs you of this: 堆栈跟踪通知您以下信息:
at GoogleMain.Google_Tc1.Open(Google_Tc1.java:13)
So to fix it assign XP
所以要解决它分配
XP
Your WebDriver XP
instrance variable is never initialized. 您的
WebDriver XP
instrance变量永远不会初始化。 The main
method in HomePage
is never called, and even if it was it wouldn't matter since it doesn't manipulate the XP
variable you're trying to use on your test. 永远不会调用
HomePage
的main
方法,即使这样也没关系,因为它不会操纵您要在测试中使用的XP
变量。
I believe this is what you want to achieve: 我相信这是您想要实现的目标:
public class HomePage {
public HomePage(WebDriver driver) {
driver = new FirefoxDriver();
driver.navigate().to("https://www.google.co.in");
driver.manage().window().maximize();
}
}
And the test 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);
}
}
Now when you create an instance of HomePage
passing a driver as an argument, the driver is initialized and can be used. 现在,当您创建将驱动程序作为参数传递的
HomePage
实例时,该驱动程序将被初始化并可以使用。
I changed the variable names because the names you used weren't good. 我更改了变量名称,因为您使用的名称不好。 You should read 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 and try to stick with it.
您可能应该在这里和这里阅读有关页面对象模型的信息 ,并尝试坚持下去。
Just like you have initialized WD in main method of Homepage class, WD = new FirefoxDriver();
就像您在Homepage类的main方法中初始化WD一样,
WD = new FirefoxDriver();
similarly you have to initialize XP before using it. 同样,您必须在使用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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.