Issue
I was trying to run my old selenium practiced scripts which were working fine a month ago and are throwing errors now, especially at the constructor
PageFactory.initElements(driver, this);
Any advice?
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.crm.qa.base.TestBase;
public class LoginPage extends TestBase {
@FindBy(name="username")`enter code here`
WebElement Username;
@FindBy(name="password")
WebElement Password;
@FindBy(xpath="//div[@class='input-group-btn']//input")
WebElement LoginBtn;
@FindBy(xpath="//a[contains(text(),'Sign Up')]")
WebElement SignUPLink;
@FindBy(xpath="//a[@class='navbar-brand']//img")
WebElement CRMPROLogo;
public LoginPage() {
PageFactory.initElements(driver, this);
}
//#Actions
public String ValidateLoginPageTitle() {
return driver.getTitle();
}
public boolean ValidateCRMlogoImg() {
return CRMPROLogo.isEnabled();
}
public HomePage ValidateLoginTest(String un, String pwd) throws InterruptedException {
Username.sendKeys(un);
Password.sendKeys(pwd);
LoginBtn.click();
Thread.sleep(3000);
return new HomePage();
}
}
Stacktrace
FAILED CONFIGURATION: @BeforeMethod SetUp
java.lang.NoSuchMethodError: 'void org.openqa.selenium.support.PageFactory.initElements(org.openqa.selenium.SearchContext, java.lang.Object)'
at com.crm.qa.pages.LoginPage.<init>(LoginPage.java:29)
at com.crm.qa.testcases.LoginPageTest.SetUp(LoginPageTest.java:24)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
Solution
NoSuchMethodError normally means you compiled with one version, but are running with a different (normally older) version, hence the Exception.
The solution is to make sure you are developing and running on the same version of Java, selenium, and any other 3rd party library you are also using
Answered By - ControlAltDel
Answer Checked By - Mildred Charles (JavaFixing Admin)