org.testng.TestNGException: произошла ошибка при создании экземпляра класса. Убедитесь, что он может быть создан / доступен - PullRequest
0 голосов
/ 02 апреля 2020

Я пишу сценарий селена, используя java, TestNG и maven. Перед использованием объектной модели страницы все работало нормально. После использования POM я получаю следующую ошибку:

org.testng.TestNGException: при создании экземпляра класса YieldStreetCartAutomation.SauceDemo.Login произошла ошибка. Проверьте, чтобы убедиться, что он может быть создан / доступен. в org.testng.internal.InstanceCreator.createInstanceUsingObjectFactory (InstanceCreator. java: 134) в org.testng.internal.InstanceCreator.createInstance (InstanceCreator. java: 79) в org.testng.mpl .ImpIIIIIIIQ . java: 109) в org.testng.internal.ClassImpl.getInstances (ClassImpl. java: 167) в org.testng.TestClass.getInstances (TestClass. java: 102) в org.testng.TestClass. initTestClassesAndInstances (TestClass. java: 82) в org.testng.TestClass.init (TestClass. java: 74) в org.testng.TestClass. (TestClass. java: 39) в org.testng.TestRunner. initMethods (TestRunner. java: 459) в org.testng.TestRunner.init (TestRunner. java: 338) в org.testng.TestRunner.init (TestRunner. java: 291) в org.testng.TestRunner ) 1031 *: 66) на org.testng.ITestRunnerFac tory.newTestRunner (ITestRunnerFactory. java: 55) по адресу org.testng.SuiteRunner $ ProxyTestRunnerFactory.newTestRunner (SuiteRunner. java: 676) по адресу org.testng.SuiteRunner.init (SuiteRunner: 1734) * 10 * 10) .testng.SuiteRunner. (SuiteRunner. java: 112) в org.testng.TestNG.createSuiteRunner (TestNG. java: 1275) в org.testng.TestNG.createSuiteRunners (TestNG. java: 1251) в организации .testng.TestNG.runSuitesLocally (TestNG. java: 1100) в org.testng.TestNG.runSuites (TestNG. java: 1039) в org.testng.TestNG.run (TestNG. java: 1007) в org.testng.remote.AbstractRemoteTestNG.run (AbstractRemoteTestNG. java: 115) в org.testng.remote.RemoteTestNG.initAndRun (RemoteTestNG. java: 251) в org.testng.remote.RemoteTestNG.main. java: 77)

Вот мой BaseTest

package YieldStreetCartAutomation.SauceDemo;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

public class BaseTest {

public  WebDriver driver;

    @BeforeSuite
    public void setup() {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\viral\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.saucedemo.com/");
        driver.manage().window().maximize();

}


    @AfterSuite
    public void teardown() {
        driver.close();
    }
}

Вот моя основная программа:

   package YieldStreetCartAutomation.SauceDemo;

import java.util.List;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.apache.commons.lang3.RandomStringUtils;


public class Login extends BaseTest
{



public Login(WebDriver driver){
    this.driver = driver;
    PageFactory.initElements(driver, this);

}


@FindBy(id = "user-name")
WebElement username;
@FindBy(id = "password")
WebElement password;
@FindBy(className = "btn_action")
WebElement submit;
@FindBy(css = "div#shopping_cart_container>a>svg>path")
WebElement cart;
@FindBy(css = "div.cart_footer :last-child")
WebElement checkout;
@FindBy(id = "first-name")
WebElement firstname;
@FindBy(id = "last-name")
WebElement lastname;
@FindBy(id = "postal-code")
WebElement zip;
@FindBy(xpath = "//input[@value='CONTINUE']")
WebElement cont;
@FindBy(css = "div.cart_footer :last-child")
WebElement finish;
@FindBy(css = "div.bm-burger-button>button")
WebElement menu;
@FindBy(id = "logout_sidebar_link")
WebElement logout;

String thankyou;

    @BeforeMethod
    public void Logon() {
        username.sendKeys("standard_user");
        password.sendKeys("secret_sauce");
        submit.click();
    }

    @Test
    public void cart() {
        String title = driver.getTitle();
        System.out.println(title);
        List<WebElement> button = driver.findElements(By.xpath("//button[text()='ADD TO CART']"));
        for(WebElement bt : button) {
            bt.click();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            }
        cart.click();
        checkout.click();
        firstname.sendKeys(RandomStringUtils.randomAlphabetic(5));
        lastname.sendKeys(RandomStringUtils.randomAlphabetic(5));
        zip.sendKeys(RandomStringUtils.randomNumeric(5));
        cont.click();
        finish.click();
        thankyou = driver.findElement(By.cssSelector("h2.complete-header")).getText();
        Assert.assertEquals(thankyou, "THANK YOU FOR YOUR ORDER");

        }


    @AfterMethod
    public void logout() {
        menu.click();
        WebElement logout = driver.findElement(By.id("logout_sidebar_link"));
        logout.click();

    }


}

POM. xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>YieldStreetCartAutomation</groupId>
  <artifactId>SauceDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SauceDemo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.1.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.0</version>
</dependency>
<dependency>
    <groupId>xml-apis</groupId>
    <artifactId>xml-apis</artifactId>
    <version>1.4.01</version>
</dependency>
  </dependencies>
</project>

Пожалуйста, помогите мне

1 Ответ

0 голосов
/ 04 апреля 2020

Это может помочь. Я создал аналогичный проект Maven с TestNG / Selenium со следующими различиями

Изменить на POM

<!-- Using TestNG so removing jUnit
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> -->

Родительский класс

import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;


public class AppTestBase {

    public RemoteWebDriver driver = null;

    @BeforeSuite
    public void setUp() {
        try {
            URL url = new URL("http://localhost:4444/wd/hub");
            DesiredCapabilities caps = DesiredCapabilities.chrome();
            driver = new RemoteWebDriver(url, caps);
        } catch (Exception e) {
            System.out.print("Exception " + e.getMessage());
        }
    }

    @AfterSuite
    public void tearDown() {
        driver.close();
    }

}

Тестовый класс

import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;

/**
 * Test that uses PageFactory
 */
public class AppTestInstance
extends AppTestBase {

    // PageFactory will set all these when the page is loaded
    @FindBy(id = "username")         WebElement username;
    @FindBy(id = "password")         WebElement password;
    @FindBy(id = "login")            WebElement loginbtn;

    @Test public void shouldLogin() {
        driver.get("http://testserver/login");

        PageFactory.initElements(driver, this);

        username.sendKeys("ausername");
        password.sendKeys("apassword");
        loginbtn.click();
    }    
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...