简体   繁体   English

如何在Selenium WebDriver中调用Java类?

[英]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. 永远不会调用HomePagemain方法,即使这样也没关系,因为它不会操纵您要在测试中使用的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.

相关问题 如何在硒webdriver中从一个java类(页面)到另一个TestNG类(test)调用下拉值? - How to call dropdown value from one java class(page) to another TestNG class(test) in selenium webdriver? Selenium WebDriver和Java Robot类 - Selenium WebDriver and Java Robot Class 如何在Selenium WebDriver中使用其父类通过partialinktext调用元素 - How to call a element by partialinktext using its parent class in selenium webdriver 如何使用Java和Selenium WebDriver从属性文件中调用方法? - How to call a method from property file using Java and Selenium WebDriver? 如何在Java中从Selenium Webdriver调用小书签? - How can I call a bookmarklet from Selenium webdriver in java? 如何使用 Selenium Java WebDriver 测试轮询 javascript ajax 调用 - How to test a polling javascript ajax call with Selenium Java WebDriver 如何<a class </a>在Selenium WebDriver中使用Java</a>单击带有标签名称的链接 - How to click a link with Tag name <a class </a> using Java in Selenium WebDriver 如何在 Java 的帮助下在 Selenium Webdriver 中使用 Button 类 - How to use Button class In Selenium Webdriver by the help of Java 如何装饰硒WebDriver类? - how to decorate the selenium WebDriver class? 有没有一种方法可以用Java在Selenium Webdriver中调用nextElementSibling? - Is there a way to call nextElementSibling in Selenium Webdriver with Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM