Selenium TestNG Page对象模型框架中的问题 [英] Issue in Selenium TestNG Page Object Model framework

查看:53
本文介绍了Selenium TestNG Page对象模型框架中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Page Object Model创建硒框架.目前,我在单独的类中有两个测试用例.如果运行testng.xml文件,则仅执行第一类.当第二个类启动时,将发生空指针异常.当我分别运行这些类时,两者都工作正常.但是,当我尝试同时运行它们时,只会执行第一类.空指针异常发生在第二个类上.

I am creating selenium framework using Page Object Model. Currently I have two test cases in separate classes. If, I run the testng.xml file, only the first class gets executed. Null pointer exception occurs when the second class is initiated. When I run the classes separately, both are working fine. But, when I try to run them both only the first class gets executed. Null pointer exception occurs on the second class.

基类.java

package com.midcities.pages;

import java.io.File;
import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.MediaEntityBuilder;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.midcities.utility.BrowserConfig;
import com.midcities.utility.ConfigDataProvider;
import com.midcities.utility.ExcelDataProvider;
import com.midcities.utility.Helper;

public class BaseClass {

    public WebDriver driver;
    public ExcelDataProvider excel;
    public ConfigDataProvider config;
    public ExtentReports reports;
    public ExtentTest logger;

    @BeforeSuite
    public void setupSuite() {

        //This is base class

        excel = new ExcelDataProvider();
        config = new ConfigDataProvider();

        ExtentHtmlReporter extent = new ExtentHtmlReporter(new File(
                System.getProperty("user.dir") + "/Reports/Midcities_" + Helper.getCurrentDateTime() + ".html"));
        reports = new ExtentReports();
        reports.attachReporter(extent);

    }

    @BeforeClass
    public void startUp() {
        **//When the second class is being run, null pointer exception occurs in this line**
        driver = BrowserConfig.startApplication(driver, config.getBrowser(), config.getUrl());

    }

    @AfterClass
    public void finish() throws InterruptedException {

         Thread.sleep(3000);

         BrowserConfig.quitBrowser(driver);

    }

    @AfterMethod
    public void screenshotHandler(ITestResult result) throws IOException {

        if (result.getStatus() == ITestResult.FAILURE) {

            // Helper.captureScreenshot(driver,result.getName());

                logger.fail("Test failed", MediaEntityBuilder
                        .createScreenCaptureFromPath(Helper.captureScreenshot(driver, result.getName())).build());

        } else if (result.getStatus() == ITestResult.SUCCESS) {

                logger.pass("Test passed", MediaEntityBuilder
                        .createScreenCaptureFromPath(Helper.captureScreenshot(driver,result.getName())).build());
        } 

        reports.flush();
    }

}

LoginPageElements.java

package com.midcities.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;

import com.paulhammant.ngwebdriver.ByAngular;

public class LoginPageElements {

    WebDriver driver;

    public LoginPageElements(WebDriver driver){

        this.driver = driver;

    }

    public void Login_Positive(String uname, String pass) {

        driver.findElement(By.id("mat-input-0")).sendKeys(uname);

        driver.findElement(By.id("mat-input-1")).sendKeys(pass);

        driver.findElement(ByAngular.buttonText("SIGN IN")).click();

    }

    public void Login_Negative(String uname, String pass) {

        String errorMessage = "Invalid Email Id or Password";

        driver.findElement(By.id("mat-input-0")).sendKeys(uname);

        driver.findElement(By.id("mat-input-1")).sendKeys(pass);

        driver.findElement(ByAngular.buttonText("SIGN IN")).click();

        String error = driver.findElement(By.cssSelector("small[class*='form-error-msg']")).getText(); 

        Assert.assertEquals(error, errorMessage);

    }
}

Login_Positive_Case.java

package com.midcities.testcases;

import org.testng.annotations.Test;

import com.midcities.pages.BaseClass;
import com.midcities.pages.LoginPageElements;

public class Login_Positive_Case extends BaseClass{

    @Test
    public void loginIntoTheApplication() {

        logger = reports.createTest("Login Positive case");

        LoginPageElements loginFunc = new LoginPageElements(driver);

        logger.info("Starting Application");

        loginFunc.Login_Positive(excel.getStringCellData("Login", 1, 0), excel.getStringCellData("Login", 1, 1));

    }

}

Login_Negative_Case.java

package com.midcities.testcases;

import org.testng.annotations.Test;

import com.midcities.pages.BaseClass;
import com.midcities.pages.LoginPageElements;

public class Login_Negative_Case extends BaseClass{

    @Test
    public void loginNegativeInput() {

        logger = reports.createTest("Login Negative case");

        LoginPageElements loginFunc = new LoginPageElements(driver);

        loginFunc.Login_Negative(excel.getStringCellData("Login", 2, 0), excel.getStringCellData("Login", 2, 1));

    }


}

BrowserConfig.java

package com.midcities.utility;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.paulhammant.ngwebdriver.NgWebDriver;

public class BrowserConfig {

    public static WebDriver startApplication(WebDriver driver, String browsername, String AppUrl) {

        if(browsername.equalsIgnoreCase("chrome")) {

            System.setProperty("webdriver.chrome.driver", "./Drivers/chromedriver.exe");

            driver = new ChromeDriver();

        } else if(browsername.equalsIgnoreCase("firefox")){

            System.setProperty("webdriver.gecko.driver", "./Drivers/geckodriver.exe");

            driver = new FirefoxDriver();

        } else {

            System.out.println("Browser not supported");
        }

        NgWebDriver ngWebDriver = new NgWebDriver((JavascriptExecutor) driver);

        ngWebDriver.waitForAngularRequestsToFinish();

        driver.manage().window().maximize();

        driver.get(AppUrl);

        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

        return driver;
    }



    public static void quitBrowser(WebDriver driver) {

        driver.quit();

    }

}

ConfigDataProvider.java

package com.midcities.utility;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

public class ConfigDataProvider {

    Properties pro;

    public ConfigDataProvider() {

        File src = new File("./Config/config.properties");

        try {
            FileInputStream fis = new FileInputStream(src);

            pro = new Properties();

            pro.load(fis);

        } catch (Exception e) {

            System.out.println("Configuration file not loaded>>" + e.getMessage());
        }
    }

    public String getDataFromConfig(String keyToSearch) {

        return pro.getProperty(keyToSearch);
    }

    public String getBrowser() {

        return pro.getProperty("Browser");
    }

    public String getUrl() {

        return pro.getProperty("stagUrl");

    }
}

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Login_Positive">
    <classes>
      <class name="com.midcities.testcases.Login_Positive_Case"/>
      <class name="com.midcities.testcases.Login_Negative_Case"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

控制台

[ERROR] Tests run: 5, Failures: 1, Errors: 0, Skipped: 3, Time elapsed: 27.505 s <<< FAILURE! - in TestSuite
[ERROR] com.midcities.testcases.Login_Negative_Case.startUp  Time elapsed: 6.407 s  <<< FAILURE!
java.lang.NullPointerException

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   Login_Negative_Case>BaseClass.startUp:52 » NullPointer
[INFO] 
[ERROR] Tests run: 5, Failures: 1, Errors: 0, Skipped: 3

推荐答案

您可以尝试指定程序包名称,而不是指定应该运行两个测试的类.

You could try specifying the package name rather than the classes which should run both of the tests.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Login_Positive">
    <packages>
      <package name="com.midcities.testcases.*"/>
    </packages>
  </test> <!-- Test -->
</suite> <!-- Suite -->

这篇关于Selenium TestNG Page对象模型框架中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆