尝试在包外部调用类的方法时看到java.lang.NullPointerException

时间:2019-01-21 08:39:00

标签: java selenium-webdriver testng

java.lang.NullPointerException可见,当我尝试调用/调用位于不同

中的方法deadlinkscheckerLPbrokenlinkstest方法中

堆栈:
Selenium Webdriver:3.141.59
Ngwebdriver:1.14
Java:10
Testng:6.10

Java程序包。下面是我的代码和错误堆栈跟踪

TestNG Runner /测试类:

    public class TestClass{
        static WebDriver driver;
        static NgWebDriver ngdriver;
        TestUtil DLC = new TestUtil();

        @BeforeMethod
        public void Initialization() {
            ngdriver = new NgWebDriver((JavascriptExecutor) driver);
            System.setProperty("webdriver.chrome.driver", ".//drivers//chromedriver.exe");
            driver = new ChromeDriver();
            driver.get("https://www.google.com/");
        }

        @Test(priority = 2, description="To Verify Broken Images and Links in Login Page")
        public void LPbrokenlinkstest() throws InterruptedException, MalformedURLException, IOException{
            DLC.deadlinkschecker();
        }

        @AfterMethod
        public void Wrapup() {
            driver.quit();
        }
    }

TestUtil类:

    public class TestUtil {
        static WebDriver driver;
        static NgWebDriver ngdriver;    

        public  void deadlinkschecker() throws MalformedURLException, IOException {
            // 1. Get the list of all the links and images
            List<WebElement> linkList = driver.findElements(By.tagName("a"));
            linkList.addAll(driver.findElements(By.tagName("img")));

            // size of linklist
            System.out.println("Size of Full Links and images-->" + linkList.size());

            List<WebElement> activelinks = new ArrayList<WebElement>();

            // 2. Iterate linkslist : exclude all the links/images - doesnt have any href
            // attribute.

            for (int i = 0; i < linkList.size(); i++) {
                System.out.println(linkList.get(i).getAttribute("href"));
                if (linkList.get(i).getAttribute("href") != null
                        && (!linkList.get(i).getAttribute("href").contains("javascript"))) {
                    activelinks.add(linkList.get(i));
                }
            }
            // get the size of active links list:
            System.out.println("Size of active Links and images-->" + activelinks.size());

            // 3. Check the Href url, with httpconnection api:

            for (int j = 0; j < activelinks.size(); j++) {

                HttpURLConnection connection = (HttpURLConnection) new URL(activelinks.get(j).getAttribute("href"))
                        .openConnection();
                connection.connect();
                String response = connection.getResponseMessage(); // returns ok for good links
                int x = connection.getResponseCode();
                connection.disconnect();
                System.out.println(activelinks.get(j).getAttribute("href") + "===>" + x + "===>" + response);
            }
        }
    }
java.lang.NullPointerException
at com.qa.test.util.TestUtil.deadlinkschecker(TestUtil.java:36)
at com.qa.ob.tests.TestClass1.LPbrokenlinkstest(TestClass1.java:71)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

0 个答案:

没有答案