dimanche 2 juillet 2017

Getting NullPointerException if I run tests through testng.xml, but it works fine one at a time

Prologue: I am new to automation testing

I have problem with two test, which I try to run through testng.xml as a suite. But if I ran them separately, they would work fine. Here is my code: BasePage.java

public class BasePage {
    protected WebDriver driver;
    public BasePage(WebDriver driver) {
        this.driver = driver;
    }
}

BaseTest.java

public class BaseTest {
    private WebDriver driver;
    private final String chromeDriverPath = "src//main//resources//chromedriver.exe";
    private final String firefoxDriverPath = "src//main//resources//geckodriver.exe";

    public WebDriver getDriver() {
        return driver;
    }

    @BeforeTest
    @Parameters({"browser", "baseURL"})
    public void setUp(@Optional String browser, @Optional String baseURL) {
        driver = getBrowserType(browser);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.navigate().to(baseURL);
    }

    private WebDriver getBrowserType(String browserType) {
        if (driver == null ) {
            if (browserType.equalsIgnoreCase("firefox")) {
                 System.setProperty("webdriver.gecko.driver", firefoxDriverPath);
                 return  new FirefoxDriver();
            }
            else if (browserType.equalsIgnoreCase("chrome")) {
                System.setProperty("webdriver.chrome.driver", chromeDriverPath);
                return new ChromeDriver();
            }
        }
        return driver;
    }

    @AfterTest
    public void tearDown() {
        if (driver != null)
            driver.quit();
    }
}

HomePage.java

public class HomePage extends BasePage {

    public HomePage(WebDriver driver) {
        super(driver);
    }

    public boolean verifyHomePage() {
        return driver.getCurrentUrl().contains("foodpanda.com");
    }
}

NavigationMenuComponent.java

public class NavigationMenuComponent extends BasePage{

    public NavigationMenuComponent(WebDriver driver) {
        super(driver);
    }

    @FindBy(xpath = "//ul[@class='right']//a[contains(@href, '/about')]")
    private WebElement aboutLink;

    public void test() {
         aboutLink.click();
    }
}

HomePageTest.java

public class HomePageTest extends BaseTest {

    private HomePage homePage;

    @BeforeClass(alwaysRun = true)
    public void init() {
        homePage = PageFactory.initElements(getDriver(), HomePage.class);
    }

    @Test(groups = "home-page")
    public void verifyFirstPage() {
        Assert.assertTrue(homePage.verifyHomePage());
    }
    }

NavigationMenuTest.java

public class NavigationMenuTest extends BaseTest{

    private NavigationMenuComponent navigationMenuComponent;

    @BeforeClass
    public void init() {
        navigationMenuComponent = PageFactory.initElements(getDriver(), NavigationMenuComponent.class);
    }

    @Test(groups = "about-page")
    public void verifyAboutLink() {
       navigationMenuComponent.test();
    }
}

And my testng.xml

<!DOCTYPE suite SYSTEM "http://ift.tt/19x2mI9">
<suite name="suite" parallel="tests" thread-count="2">
    <parameter name="baseURL" value="http://ift.tt/1vvHBtP"></parameter>
    <test name="chromeTest" >
        <parameter name="browser" value="chrome"></parameter>
        <classes>
            <class name="tests.HomePageTest"></class>
            <class name="tests.NavigationMenuTest"></class>
        </classes>
    </test>
    <test name="firefoxTest">
        <parameter name="browser" value="firefox"></parameter>
        <classes>
            <class name="tests.HomePageTest"></class>
            <class name="tests.NavigationMenuTest"></class>
        </classes>
    </test>
</suite>

Here is stacktrace I am stuck on this one. Should i run them seperately or i am doing smh wrong?

Aucun commentaire:

Enregistrer un commentaire