I have 2 TestNG classes which are:
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); } }
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?
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(); } }