Файл определений шага огурца не работает, аннотации TestNG работают нормально - PullRequest
1 голос
/ 05 ноября 2019

Я начал писать пример кода cucumber-testng и столкнулся с проблемой с файлом определения шага, который не запущен. Даже мой автоматический браузер не запускается. Вот скриншот иерархии моего проекта: https://prnt.sc/psasmt

Ниже приведены мои 5 файлов:

  Feature: Apple Airpods

  Scenario Outline: Apple Airpods scenario tests

  Given user is already on apple website
  Then direct to ipad
  When title is iPad - Apple
  Then direct to ipad air
  Then search for airpods by keyword
  When title is AirPods - Apple
  Then print welcome to the airpods official webpage

Файл StepDefinition

    package stepDefinitions;

    import java.util.concurrent.TimeUnit;

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

    import cucumber.api.java.en.Given;
    import cucumber.api.java.en.Then;
    import cucumber.api.java.en.When;


    public class AppleStepDefinitions {

        WebDriver driver;

        @Given("^user is already on apple website$")
        public void user_is_already_on_apple_website()
        {
            System.out.println("Opening the web app");
            System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lenovo- 
            I7\\Desktop\\chromedriver.exe");
            System.out.println("making connection to driver");
            driver = new ChromeDriver();
            driver.get("https://www.apple.com/");   
        }


        @Then("^direct to ipad$")
        public void direct_to_ipad()
        {
            WebElement ipad = driver.findElement(By.xpath("//*[@id=\'ac- 
             globalnav\']/div/ul[2]/li[3]"));
            ipad.click();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        }

        @When("^title is iPad - Apple$")
        public void title_is_iPad_Apple()
        {
             String title = driver.getTitle();
             System.out.println("iPad Page title ::"+ title);
             Assert.assertEquals("iPad - Apple", title);
        }

        @Then("^direct to ipad air$")
        public void direct_to_ipad_air()
        {
            WebElement ipadAir= 
        driver.findElement(By.cssSelector("#chapternav > div > ul > 
        li.chapternav-item.chapternav-item-ipad-air > a"));
            ipadAir.click();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);        
        }

        @Then("^search for airpods by keyword$")
        public void search_for_airpods_by_keyword()
        {
            WebElement search = driver.findElement(By.id("ac-gn-link- 
         search"));
            search.click();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);    

            driver.findElement(By.id("ac-gn-searchform- 
            input")).sendKeys("airpods");
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

            driver.findElement(By.xpath("//* 
         [@id=\"quicklinks\"]/li[1]/a")).click();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        }

        @When("^title is AirPods - Apple$")
        public void title_is_AirPods_Apple()
        {
             String title = driver.getTitle();
             System.out.println("Airpods Page title ::"+ title);
             Assert.assertEquals("AirPods - Apple", title);
        }

        @Then("^print welcome to the airpodsofficial webpage$") 
        public void print_message()
        {
            System.out.println("Welcome to airpod official web page");
            driver.quit();
        }


    }

Runner.java

    package testRunner;

    import org.testng.annotations.AfterClass;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;

    import cucumber.api.CucumberOptions;
    import cucumber.api.testng.CucumberFeatureWrapper;
    import cucumber.api.testng.TestNGCucumberRunner;

    @CucumberOptions(
        features = "src/test/java/FeatureFile",
        glue = {"stepDefinitions"},
        tags = {"~@Ignore"},
        format = {
                        "pretty",
                        "html:target/cucumber-reports/cucumber-pretty",
                        "json:target/cucumber-reports/CucumberTestReport.json",
                        "rerun:target/cucumber-reports/rerun.txt"
                },plugin = "json:target/cucumber-reports/CucumberTestReport.json")


    public class Runner {


        private TestNGCucumberRunner testNGCucumberRunner;

        @BeforeClass
        public void setUp() throws Exception
        {
            testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
        }


        @Test(groups = "cucumber", description="Runs Apple Feature", dataProvider="features")
          public void my_test(CucumberFeatureWrapper cucumberFeature) 
        {
            testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());
        }


        @DataProvider
        public Object[][] features()
        {
            return testNGCucumberRunner.provideFeatures();      
        }


        @AfterClass
        public void tearDown()
        {
            testNGCucumberRunner.finish();  
        }




    }

TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="TestNG-Cucumber Suite" parallel="tests" thread-count="10">
    <test name="Apple test">
        <classes>
            <class name="testRunner.Runner" />
        </classes>
    </test>
</suite>

А вот вывод, который я получаю на консоли.

    [RemoteTestNG] detected TestNG version 6.14.3
    Feature: Apple Airpods

      Scenario Outline: Apple Airpods scenario tests       [90m# apple_feature.feature:3[0m
        [36mGiven [0m[36muser is already on apple website[0m
        [36mThen [0m[36mdirect to ipad[0m
        [36mWhen [0m[36mtitle is iPad - Apple[0m
        [36mThen [0m[36mdirect to ipad air[0m
        [36mThen [0m[36msearch for airpods by keyword[0m
        [36mWhen [0m[36mtitle is AirPods - Apple[0m
        [36mThen [0m[36mprint welcome to the airpods official webpage[0m

    0 Scenarios
    0 Steps
    0m0.000s


    ===============================================
    TestNG-Cucumber Suite
    Total tests run: 1, Failures: 0, Skips: 0
    ===============================================
...