i m learning TestNG,so i have created one Base class in that i have create object for chrome browser.
this is my base class
public class BaseClass {
public String url="https://dofdev-services.azurewebsites.net/";
public String username1="info#gravityconsulting.com.au";
public String password1="Gravity#123";
public WebDriver driver;
#BeforeClass
public void setUp()
{
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir" +"/Drivers/chromedriver.exe"));
driver=new ChromeDriver();
}
#AfterClass()
public void tearDown()
{
driver.quit();
}
}
this is my test case class
public class TC_LoginPage_001 extends BaseClass{
#Test
public void loginTestCase()
{
driver.get(url);
LoginPagePOM lp=new LoginPagePOM(driver);
lp.setUserName(username1);
lp.setPassword(password1);
lp.clickSignIn();
if(driver.getTitle().equalsIgnoreCase(""))
{
Assert.assertTrue(true);
}else {
Assert.assertTrue(false);
}
}
this is my another class here i have created the constructor(pom class)
public class LoginPagePOM {
WebDriver driver;
public LoginPagePOM(WebDriver driver)
{
this.driver=driver;
PageFactory.initElements(driver, this);
}
#FindBy(id="userName")
WebElement username;
#FindBy(name ="passwords")
WebElement password;
#FindBy(name="btn-sdz-login")
WebElement click;
public void setUserName(String usname)
{
username.sendKeys(usname);
}
public void setPassword(String Pass)
{
password.sendKeys(Pass);
}
public void clickSignIn()
{
click.click();
}
}
this is exception stack trace (getting null pointer exception)
Make sure that your constructor has driver object passed in loginpage. You need to pass the driver to all pageclasses. Also login page didnt extend baseclass. You need to extend as below and also remove the webdriver instantiation in loginpage.
Ex:
public class LoginPagePOM extends BaseClass {
public LoginPagePOM (WebDriver driver)
{
this.driver=driver;
}
public void setUserName(){
........
}
public void setPassword(){
........
}
}
You have a driver in your BaseClass and then you are again making a driver in the LoginPagePOM class, due to which you are getting NPE.
To solve this problem, you have make the driver static and then use that single driver in all the classes where you need it. You dont need to initialise it again.
You can use public static WebDriver driver; in your BaseClass and then you can use BaseClass.driver in all you classes where you need it.
Your TC_LoginPage_001 would be like:
public class TC_LoginPage_001 extends BaseClass{
#Test
public void loginTestCase()
{
BaseClass.driver.get(url);
LoginPagePOM lp=new LoginPagePOM(driver);
lp.setUserName(username1);
lp.setPassword(password1);
lp.clickSignIn();
if(BaseClass.driver.getTitle().equalsIgnoreCase(""))
{
Assert.assertTrue(true);
}else {
Assert.assertTrue(false);
}
}
And your LoginPagePOM would be like:
public class LoginPagePOM {
public LoginPagePOM()
{
PageFactory.initElements(BaseClass.driver, this);
}
#FindBy(id="userName")
WebElement username;
#FindBy(name ="passwords")
WebElement password;
#FindBy(name="btn-sdz-login")
WebElement click;
public void setUserName(String usname)
{
username.sendKeys(usname);
}
public void setPassword(String Pass)
{
password.sendKeys(Pass);
}
public void clickSignIn()
{
click.click();
}
}
Related
I spent two days on this, and can't seem to find where is the error. In a separate Class, the code is working. In pom.xml I get an error: NullPointerException: Cannot invoke LoginPage.loginCustomer() because this.loginPage is null.
public class _BasePage {
private WebDriver driver;
private WebDriverWait driverWait;
}
with parametrised construktor, getters and setters. Then, a class BankManagerMenu:
public class BankManagerMenu extends _BasePage {
private By addCustomer = By.xpath("/html/body/div/div/div[2]/div/div[1]/button[1]");
public BankManagerMenu(WebDriver driver, WebDriverWait driverWait){
super(driver,driverWait);
}
public boolean addCustomerBtn(){
return this.getDriver().findElement(this.addCustomer).isDisplayed();
}
And a Class LoginPage
public class LoginPage extends _BasePage {
private By customerButton = By.xpath("//*[contains(text(), 'Customer Login']");
private By bankManagerButton = By.xpath("//*[contains(text(), 'Bank Manager Login')]");
public LoginPage(WebDriver driver, WebDriverWait driverWait) {
super(driver, driverWait);
}
public void loginBankManager() {
new WebDriverWait(getDriver(), Duration.ofSeconds(20)).until(ExpectedConditions.elementToBeClickable(bankManagerButton)).click();
}
public void loginCustomer() {
this.getDriver().findElement(this.customerButton).click();
}}
Finally, this is my Test Class
public class TestPage {
private WebDriver driver;
private WebDriverWait driverWait;
private LoginPage loginPage;
private BankManagerMenu bankManagerMenu;
public TestPage(){}
#BeforeClass
public void beforeClass(){
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Raven\\Desktop\\QA\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
// WebDriverWait driverWait = new WebDriverWait(driver, Duration.ofSeconds(15));
driver.navigate().to("https://www.globalsqa.com/angularJs-protractor/BankingProject/#/login");
}
#Test
public void loginAsBankManager(){
this.loginPage.loginBankManager();
// Assert.assertTrue(this.bankManagerMenu.addCustomerBtn());
}}
I am new to Selenium, so basically whenever I ran my test, it would open up the URL which I have mentioned in my test. In my same Test I have also mentioned to fill the username and Password.
But somehow once the browser gets launched and redirect to the URL, it opens up another instance of blank browser failing my script that element not found.
Please help me here.
///////////////////////////////////////////////////////////////////////////
public class TruefillTest extends BaseClass {
public Truefill truefill()
{
WebDriver driver=InitializeDriver();
return new Truefill(driver);
}
#Test
public void userLoginIntoTheSystem()
{
truefill().dashBoard().Url();
truefill().dashBoard().EnterUsername("bjdgfe#swcwr.com");
truefill().dashBoard().EnterPassword("Test1234");
}
///////////////////////////////////////////////
public class Truefill {
private WebDriver driver;
public Truefill(WebDriver driver) {
this.driver=driver;
}
public DashBoardPage dashBoard()
{
return new DashBoardPage(driver);
}
////////////////////////////////////////////////////////////
public class DashBoardPage {
private final WebDriver driver;
By Username= By.xpath("//input[#name='name']");
By Password= By.xpath("//input[contains(#id,'exampleInputPassword1')]");
public DashBoardPage(WebDriver driver) {
this.driver=driver;
}
public void Url()
{
driver.get("https://rahulshettyacademy.com/angularpractice/");
}
public void EnterUsername(String username)
{
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(Username).sendKeys(username);
}
public void EnterPassword(String password)
{
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(Password).sendKeys(password);
}
////////////////////////////////////////////////////////////
public class BaseClass {
WebDriver driver;
public WebDriver InitializeDriver()
{
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
return driver;
}
}
Each call to the truefill() method initializes a new instance of WebDriver. Since your test is calling it multiple times, it will start a new browser instance on each line. Instead, store the DashboardPage in a local variable:
#Test
public void userLoginIntoTheSystem() {
DashBoardPage dashBoardPage = truefill().dashBoard();
dashBoardPage.Url();
dashBoardPage.EnterUsername("bjdgfe#swcwr.com");
dashBoardPage.EnterPassword("Test1234");
}
You might also want to use a setup method to initialize the Truefill instance, rather than creating it on demand:
private Truefill truefill;
#BeforeEach
public void initializeTruefill() {
WebDriver driver = InitializeDriver();
truefill = new Truefill(driver);
}
#Test
public void userLoginIntoTheSystem() {
DashBoardPage dashBoardPage = truefill.dashBoard();
dashBoardPage.Url();
dashBoardPage.EnterUsername("bjdgfe#swcwr.com");
dashBoardPage.EnterPassword("Test1234");
}
This assumes JUnit 5. If you're using JUnit 4, the annotation is #Before instead of #BeforeEach.
I would really like to understand why when approaching locating an element the exact same way as previously failing.
NoSuchElementException : Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(text(),'Add')]"}
I have also tried to use something like the following but it also failed, so in order for me write a good automation code i need to get my knowledge straight with WebDriverWait, so i would appreciate somebody with knowledge to spend some time on this inquiry.
It does locates if i have xpath like this :
//body/div[#id='root']/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[2]/div[1]/div[2]/div[1]
public void clickOnAddVehicleButton() {
wait.until(ExpectedConditions.visibilityOf(addVehicleButton));
vehicles.addVehicleButton.click();
}
I am not new into Java but i am new into automation hence some areas of knowledge is missing, i ha
Base class:
public class BasePage {
protected final WebDriver driver;
protected final WebDriverWait wait;
public BasePage(WebDriver driver) {
this.driver = driver;
this.wait = new WebDriverWait(driver,5);
PageFactory.initElements(new AjaxElementLocatorFactory(driver, 10), this);
}
}
The component where that button should be present :
public class Vehicles extends BasePage {
private static Vehicles vehicles;
private Vehicles(WebDriver driver) {
super(driver);
}
#FindBy(xpath = "//span[text(),'Add']")
public WebElement addVehicleButton;
public static Vehicles vehiclesInstance(WebDriver driver) {
return vehicles == null ? vehicles = new Vehicles(driver) : vehicles;
}
public void clickOnAddVehicleButton() {
vehicles.addVehicleButton.click();
}
Function in the service class:
public void addVehicle(String vehicleName, String numberPlate) {
myAccountService.myAccountPage()
.clickOnVehiclesTab();
vehicles.fillInVehicleName(vehicleName)
.fillInVehicleNumberPlate(numberPlate)
.clickOnAddVehicleButton();
}
WebDriverSettings for test :
public class WebDriverSettings {
protected WebDriver driver;
#Before
public void setUp() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
loginToDevEnvironment();
}
And finally my test class where i am calling the service method:
public class VehicleServiceTest extends WebDriverSettings {
private VehiclesService vehiclesService;
#Before
public void setUp(){
super.setUp();
CookieService.cookieServiceInstance(driver).acceptCookies();
HomePageService.homePageServiceInstance(driver).clickOnMyAccountTab();
LoginService.loginServiceInstance(driver).login(loginCorrectData());
HomePageService.homePageServiceInstance(driver).clickOnMyAccount();
vehiclesService = VehiclesService.vehiclesServiceInstance(driver);
}
#Test
public void shouldAddVehicle(){
vehiclesService.addVehicle("test-vehicle","test-test");
Assert.assertEquals(1,vehiclesService.getVehicles().listOfAddedVehicles().size());
}
I had a Base WebDriver class where I had made an interface where I made all implementations, if I create a constructor in the class
public class Base {
public InterfaceClass driver;
public void setDriver(InterfaceClass driver){
this.driver = driver;
}
}
public class Seleniumclass implements InterfaceClass {
private WebDriver driver;
public void initiTest(){
Browser initialisation
}
}
public interface InterfaceClass {}
Two classes: The first browser opened instance has to be passed through the second class - how to pass it in?
public class firstclass extends HomePageComponents {
#BeforeClass
public void setup() throws IOException {
driver = initiTest(this.getClass().getSimpleName());
}
public class SecondClass extends HomePageComponents {
public SecondClass(ActionEngine driver) {
// TODO Auto-generated constructor stub
System.out.println(BaseClass.driver);
driver = BaseClass.driver;
}
#BeforeClass
public void setup() throws IOException {
SecondClass ASA= new SecondClass(BaseClass.driver);
}
Please use a Singleton class like below:
public class TestBotApp
{
private static volatile TestBotApp ourInstance;
private static final Object mutex = new Object();
private WebDriver webDriver;
//public final HelperClass helperClass;
private TestBotApp() {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"/Chrome/chromedriver");
ChromeOptions opt = new ChromeOptions();
opt.addArguments("disable-extensions");
opt.addArguments("--start-maximized");
webDriver = new ChromeDriver(opt);
//webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
webDriver.navigate().to("http://localhost:4200/web");
webDriver.manage().window().maximize();
//helperClass = new HelperClass();
}
public WebDriver getWebDriver() {
return webDriver;
}
public void closeWebDriver() {
webDriver.close();
webDriver.quit();
}
public static TestBotApp getInstance() {
TestBotApp result = ourInstance;
if (result == null) {
synchronized (mutex) {
result = ourInstance;
if (result == null)
ourInstance = result = new TestBotApp();
}
}
return result;
}
}
And for create object:
public class TestConfig {
#BeforeSuite
public void testBeforeSuite() {
WebDriver webDriver = TestBotApp.getInstance().getWebDriver();
System.out.println("testBeforeSuite()");
}
#AfterSuite
public void testAfterSuite() {
System.out.println("testAfterSuite()");
//TestBotApp.getInstance().closeWebDriver();
}
#BeforeTest
public void testBeforeTest() {
System.out.println("testBeforeTest()");
}
#AfterTest
public void testAfterTest() {
System.out.println("testAfterTest()");
}
#BeforeMethod
public void beforeTestMethod() {
System.out.println("Test");
}
}
I have 3 classes like below but when I try to run my test I get NullPointer (TestPage->input1). On debug I spotted that I have a second instantion of driver which is null. Can anyone help me what I did wrong and how to fix it? Shouldn't it work properly?
public class BaseScenario {
protected WebDriver driver;
#BeforeMethod
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Ed\\Desktop\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://toolsqa.com/automation-practice-form/");
}
#AfterMethod
public void TearDown() {
driver.quit();
}
}
'
public class TestsPage {
WebDriver driver;
public TestsPage(WebDriver driver)
{
this.driver=driver;
}
public void input1(){
driver.findElement(By.xpath("//*[#id='content']/form/fieldset/div[1]/input[1]")).sendKeys("Kuba");;
}
public WebElement input2(){
WebElement input2 = driver.findElement(By.xpath("//*[#id='content']/form/fieldset/div[1]/input[2]"));
return input2;
}
}
'
public class Tests extends BaseScenario{
TestsPage pagee = new TestsPage(driver);
#Test
public void TC1() throws Exception {
pagee.input1();
pagee.input2().sendKeys("Chudy");
}
}