Reputation:
Problem: I am a beginner with selenium and automation testing and I am writing a selenium script using java, TestNG, and maven. When I write everything in one class, all works fine, but I want to have a package for all objects, a package for tests, and Base Class with the main setting. The project will contain a class for every page from the website and a class for all tests. When I try to modify something appear another error is from the constructor in BaseClass and I have no idea why The base class is this:
What I tried
public BaseClass(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
or PagegeFactory.initElements(new AjaxElementLocatorFactory(driver, TimeoutValue), this);
and didn't work.
package com.selenium.Base;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
import page_objects.AnalizesiPreturi;
import java.util.concurrent.TimeUnit;
public class BaseClass {
public WebDriver driver;
protected static AnalizesiPreturi baseClass;
public BaseClass(WebDriver driver) { }
public BaseClass() { }
@BeforeMethod
public void setUp() {
System.setProperty("webdriver.driver.chrome", "C://SeleniumWebdrivers//chromedriver.exe");
driver = new ChromeDriver();
//implicit wait
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.MINUTES);
//launch URL
driver.get("http://googl.com/");
//scroll
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,500)", "");
//WebDriverWailt for all objects located by class name
WebDriverWait wbd = new WebDriverWait(driver, 1000);
wbd.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("AnalizesiPreturi")));
}
@AfterMethod
public void tearDown(){
driver.close();
}
}
Page objects class is this from another package
package page_objects;
import com.gargoylesoftware.htmlunit.Page;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import com.selenium.Base.BaseClass;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
public class AnalizesiPreturi extends BaseClass {
public AnalizesiPreturi(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
@FindBy(css = "#onetrust-accept-btn-handler")
WebElement cookie;
@FindBy(css = "#footable_501 > tbody > tr.ninja_table_row_0.nt_row_id_77 > td.ninja_column_1.ninja_clmn_nm_pret.footable-last-visible")
WebElement plus1Button;
@FindBy(css = "#footable_501 > tbody > tr.ninja_table_row_1.nt_row_id_78 > td.ninja_column_1.ninja_clmn_nm_pret.footable-last-visible")
WebElement plus2Button;
@FindBy(css = "#calculator > div.body > div.products > div.innerProducts > div > div.right")
WebElement minusButton;
@FindBy(css = "#show-hide > div.hide")
WebElement closeButton;
@FindBy(css = "#footable_501 > thead > tr.footable-filtering > th > form > div > div > input")
WebElement search;
@FindBy(css = "#footable_501 > thead > tr.footable-filtering > th > form > div > div > div > button.btn.btn-primary")
WebElement findButton;
@Test
public AnalizesiPreturi addAnalises(){
PageFactory.initElements(driver, this);
cookie.click();
plus1Button.click();
plus2Button.click();
closeButton.click();
return null;
}
}
And the last one is the Test package
package Test;
import com.selenium.Base.BaseClass;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import page_objects.AnalizesiPreturi;
import page_objects.Centers;
public class TestsPage extends BaseClass {
public TestsPage(WebDriver driver) {
super(driver);
}
public WebDriver getDriverInstance() {
return driver;
}
@Test
public void TestAnalises()throws Exception{
//Tests
driver.get("");
AnalizesiPreturi analise = new AnalizesiPreturi(driver);
analise.addAnalises();
}
public void TestCenters(){
Centers centre = new Centers(driver);
centre.checkCenters();
}
}
The below is the received exception:
org.testng.TestNGException:
An error occurred while instantiating class Test.TestsPage. Check to make sure it can be instantiated
at org.testng.internal.InstanceCreator.createInstanceUsingObjectFactory(InstanceCreator.java:134)
at org.testng.internal.InstanceCreator.createInstance(InstanceCreator.java:79)
at org.testng.internal.objects.SimpleObjectDispenser.dispense(SimpleObjectDispenser.java:25)
at org.testng.internal.objects.GuiceBasedObjectDispenser.dispense(GuiceBasedObjectDispenser.java:30)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:112)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:165)
at org.testng.TestClass.getInstances(TestClass.java:122)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:102)
at org.testng.TestClass.init(TestClass.java:94)
at org.testng.TestClass.<init>(TestClass.java:59)
at org.testng.TestRunner.initMethods(TestRunner.java:463)
at org.testng.TestRunner.init(TestRunner.java:339)
at org.testng.TestRunner.init(TestRunner.java:292)
at org.testng.TestRunner.<init>(TestRunner.java:183)
at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:596)
at org.testng.SuiteRunner.init(SuiteRunner.java:173)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:107)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1300)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1276)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1125)
at org.testng.TestNG.runSuites(TestNG.java:1063)
at org.testng.TestNG.run(TestNG.java:1031)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
OR
java.lang.NoSuchMethodError: com.google.common.util.concurrent.SimpleTimeLimiter.create(Ljava/util/concurrent/ExecutorService;)Lcom/google/common/util/concurrent/SimpleTimeLimiter; at org.openqa.selenium.net.UrlChecker.<init>(UrlChecker.java:62) at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:197)
Upvotes: 1
Views: 2365
Reputation: 4935
You need to define a default constructor for TestsPage
. (Not just for the base class)
TestNG tries to create an object of the test class by the following methods:
Loops all constructors:
@Parameters
@Factory
.If there are no such constructors then:
So currently your code fails the above three criteria. Hence the exception.
Try adding a default constructor and it would work.
public TestsPage() {
}
Upvotes: 0