3

I have 2 TestNG classes which are:

  1. ProductCanvas

    package objectRepo;
    
    import java.util.List;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.testng.annotations.Test;
    
    public class ProductCanvas {
        private WebDriver driver;
        private By product = By.tagName("app-product-item");
        private By productName = By.xpath("//span[@itemprop='name']");
        private By addToCartButton = By.xpath("//span[contains(text(), 'Add to Cart')]");
        private By productImage = By.xpath("//img[@itemprop='image']");
        private By favIcon = By.cssSelector(".fa.fa-heart");
        private By price = By.xpath("//meta[@itemprop='price']"); 
    
        public ProductCanvas(WebDriver driver) {
            this.driver = driver;
        }
    
        public WebElement getProduct() {
            return driver.findElement(product);
        }
    
        public WebElement getProductName() {
            return driver.findElement(productName);
        }
    
        public List<WebElement> getProductNames() {
            return driver.findElements(productName);   
        }
    
        public WebElement getAddToCartButton() {
            return driver.findElement(addToCartButton);
        }
    
        public List<WebElement> getAddToCartButtons() {
            return driver.findElements(addToCartButton);   
        }
    
        public WebElement getProductImage() {
            return driver.findElement(productImage);
        }
    
        public WebElement getFavIcon() {
            return driver.findElement(favIcon);
        }
    
        public List<WebElement> getfavIcons() {
            return driver.findElements(favIcon);   
        }
    
        public WebElement getPrice() {
            return driver.findElement(price);
        }
    
        public List<WebElement> getPrices() {
            return driver.findElements(price);   
        }
    }
    
  2. ProductTC

    package productTestCases;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    import objectRepo.Cart;
    import objectRepo.ProductCanvas;
    
    public class ProductTC {
        private WebDriver driver;
        private String baseUrl = "http://test.com";
    
        public ProductTC(WebDriver driver) {
            this.driver = driver;
        }
    
        @BeforeTest
        public void openBrowser() {
            System.setProperty("driver.webdriver.chrome", "C://chromedriver.exe");
            driver = new ChromeDriver();
            driver.get(baseUrl);
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        }
    
        @Test
        public void test1() {
            ProductCanvas pc = new ProductCanvas(driver);
            int count = pc.getPrices().size();
            System.out.println(count);
        }
    }
    

When I try to run ProductTC, nothing happens and this is displayed on console:

===============================================
    Default test
    Tests run: 0, Failures: 0, Skips: 0
===============================================

But when I remove the ProductTC's constructor it runs. How would I be able to run ProductTC without removing the constructor given that another class below would need to call its method?

  1. LandingPageTestCases

    package productTestCases;
    
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    
    import objectRepo.Cart;
    
    public class LandingPageTestCases {
        private WebDriver driver;
        private String baseUrl = "http://test.com";
    
        public LandingPageTestCases(WebDriver driver) {
            this.driver = driver;
        }
    
        @BeforeTest
        public void openBrowser() {
            System.setProperty("driver.wedriver.chrome", "C://chromedriver.exe");
            driver = new ChromeDriver();
            driver.get(baseUrl);
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        }
    
        @Test
        public void addProductsToCart() throws InterruptedException {
            ProductTC ptc = new ProductTC(driver);
            ptc.test1();
        }
    }
    

2 Answers 2

2

Extract the common code into a separate helper class (not a test class). Then have both test classes instantiate the helper and call its method to do the common task.

You could extract the common code into a base class, but base classes tend to become tangled or bloated as different subclasses demand different behavior from the base class.

0

My suggestion would be instead of trying to use the ProductTC class in LandingPageTestCases class.

You can just directly use the page object methods of ProductCanvas in the LandingPageTestCases class by creating its instance as you have done above:

class LandingPageTestCases {
...
@BeforeTest
...

    @Test
    public void addProductsToCart() throws InterruptedException {
      ProductCanvas pc = new ProductCanvas(driver);
      int count = pc.getPrices().size();
      System.out.println(count);
    }
}

By doing such way you would also avoid any dependency between your test cases.

2
  • Yes, that works fine, but I was wondering if there is a way to avoid writing the same code again in another method.
    – Kenzie008
    Commented Aug 28, 2017 at 8:44
  • I think the basic navigation (to a page) will be repeated in the each test method
    – the_coder
    Commented Aug 28, 2017 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.