NoSuchWindowException: no such window: target window already closed - java

I am running a test using selenium WebDriver and getting following error:
org.openqa.selenium.NoSuchWindowException: no such window: target window already closed
from unknown error: web view not found
(Session info: chrome=50.0.2661.102)
The code I wrote is:
#Test
public void LogIn_Page() throws InterruptedException {
driver.get(config.getApplicationUrl());
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("========== Login Test Start =========");
login.loginAction("username", "password");
}
public class LogIn_Page {
static WebDriver driver;
#FindBy(how = How.CLASS_NAME, using = "button-login")
public WebElement buttonLogin;
#FindBy(how = How.CSS, using = "input[class='is-text']")
public WebElement userName;
#FindBy(how = How.NAME, using = "j_password")
public WebElement passWord;
#FindBy(how = How.CSS, using = "button[class='login__button']")
public WebElement submit;
#FindBy(how = How.ID, using = "login-popup")
public WebElement loginPop;
public LogIn_Page(WebDriver driver) {
this.driver = driver;
}
public void loginAction(String UserName, String PassWord) {
buttonLogin.click();
userName.sendKeys(UserName);
passWord.sendKeys(PassWord);
submit.click();
}
}
The test I want to do:
Press the login button which creates a log in popup window
Enter email, password,
Press login.
The popup window appear, but the values of email and password not written (exception attached)

Related

Driver giving Null point exception

The below code was trying to run without example keyword in cucumber, but the output is dispalying as a Null pointer error
WebDriver driver;
#Given("^user is alredy in login page$")
public void user_is_alredy_in_login_page() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\swagatika.mohapatra\\OneDrive - Qualitest Group\\Desktop\\selenium\\DRIVER\\D-v-88-chrome\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://opensource-demo.orangehrmlive.com/");
driver.manage().window().maximize();
}
#Given("^user enters \"(.*)\" and \"(.*)\"$")
public void user_enters_valid_user_name(String username, String password) {
this.driver = driver;
driver.findElement(By.id("txtUsername")).sendKeys(username);
driver.findElement(By.id("txtPassword")).sendKeys(password);
}
console output in Debug mode -
this = {LoginStepDefination#3223}
driver = null
username = "Admin"
password = "admin123"
this.driver = null
driver = null
WebDriver driver = new ChromeDriver();
you are redeclaring driver as a local varaible in first step instead:
WebDriver driver;
#Given("^user is alredy in login page$")
public void user_is_alredy_in_login_page() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\swagatika.mohapatra\\OneDrive - Qualitest Group\\Desktop\\selenium\\DRIVER\\D-v-88-chrome\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://opensource-demo.orangehrmlive.com/");
driver.manage().window().maximize();
}
#Given("^user enters \"(.*)\" and \"(.*)\"$")
public void user_enters_valid_user_name(String username, String password) {
this.driver.findElement(By.id("txtUsername")).sendKeys(username);
this.driver.findElement(By.id("txtPassword")).sendKeys(password);
}

Selenium PageObject throws Invocation Target Exception

I am trying to create a framework(Selenium+TestNg+java) for a Web app(The environment is MacOs+ChromeDriver and the driver server is in \usr\local\bin) but got stuck in basic structure. I have a class(Driversetup.java) that starts the browser, another one that contains WebElements and methods(ProfileUpdateObjects.java) and the third one containing test methods. Now, when I try to run this TestNG class having just a single method, I get following exception.
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at org.openqa.selenium.support.PageFactory.instantiatePage(PageFactory.java:138).
Below is the code (All the classes are in different packages).
public class ProfileUpdateTest {
#Test(enabled = true, priority = 1)
public void profileUpdate() throws MalformedURLException, InterruptedException, ParseException {
WebDriver driver = DriverSetup.startBrowser("chrome");
ProfileUpdateObjects pu = PageFactory.initElements(driver, ProfileUpdateObjects.class);
pu.navigateProfile();
}
}
The code for ProfileUpdateObject class
public class ProfileUpdateObjects {
WebDriver driver;
public ProfileUpdateObjects(WebDriver cdriver) {
this.driver = cdriver;
}
#FindBy(xpath = " //div[#class='ico-menu']")
private WebElement menu;
#FindBy(xpath = "//a[#title='My Dashboard']")
private WebElement myDashboard;
#FindBy(xpath = " //a[contains(text(),'View Profile')]")
public WebElement profile;
#FindBy(xpath = "//li[contains(text(),'Permanent Address')]")
private WebElement permanentAddress;
#FindBy(xpath = "//li[contains(text(),'Banking Information')]")
private WebElement bankingInformation;
WebDriverWait waitfor = new WebDriverWait(driver, 2000);
public void navigateProfile() throws InterruptedException {
menu.click();
profile.click();
waitfor.until(ExpectedConditions.visibilityOf(permanentAddress));
}
}
DriverSetup.java
public class DriverSetup {
public static WebDriver driver;
public static WebDriver startBrowser(String browserName, String url) {
if (browserName.equalsIgnoreCase("chrome")) {
driver = new ChromeDriver();
}
driver.manage().window().maximize();
driver.get(url);
return driver;
}
}
It is failing in pu.navigateProfile() call. Also, is it true that #FindBy takes more memory compared to driver.find() syntax and besides POM are there any other design principles for Automation framework because most of the resources over Web are one or the other implementation of POM.
Simple solution is to move new WebDriverWait. It should not be instantiated as instance variable.
Instead of:
WebDriverWait waitfor = new WebDriverWait(driver, 2000);
public void navigateProfile() throws InterruptedException {
menu.click();
profile.click();
waitfor.until(ExpectedConditions.visibilityOf(permanentAddress));
}
Use:
public void navigateProfile() {
menu.click();
profile.click();
new WebDriverWait(driver, 2000).until(ExpectedConditions.visibilityOf(permanentAddress));
}
This will solve your issue (Already tested it)

How to click on email, that contain specific text

Please help to improve my code. I use JUnit and PageObjectPattern.
I need to:
in area find email that contains String ("Test"), test fails if email not found
click on this email and find there Text ("Success")
Page:
#FindBy(className = "123")
WebElement area;
#FindBy(xpath = "//*[contains(text(), 'Test')]")
WebElement email;
public String findText(){
return area.getText();
}
public void clickEmail(){
email.click();
}
Test:
#Test
public void goTest() {
home = new Home(driver);
Assert.assertTrue("Failed", home.findText().contains("Test"));
home.clickEmail();
assertTrue(home.getElement().contains("Success"));
}
I get an error: org.openqa.selenium.ElementNotVisibleException: element not visible

NullPointerException when running my test with page factory

I get an exception when I run my test. I am using selenium with page factory. When I run following code ,it will open up the website and fail with exception below. it doesn't perform the HomePage.ClickbtnCookieWarning() in my test case.
Can someone please help me to understand why my code isn't working?
FAILED CONFIGURATION: #BeforeTest SetUp java.lang.NullPointerException
at
org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at
org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy5.click(Unknown Source) at
pageObjects.HomePage.ClickLoginLink(HomePage.java:57) at
myaccountsuite.TC1DefaultDeliveryAddDisplay.SetUp(TC1DefaultDeliveryAddDisplay.java:29)
Home Page page object
public class HomePage {
WebDriver driver;
public HomePage (WebDriver driver)
{
this.driver=driver;
}
#FindBy(id="ctl00_header_hdrCookieWarning_btnHideCookieWarning")
WebElement btnCookieWarning;
#FindBy(xpath=".//*#id='ctl00_masterContainerTop_Block_637_LoginView1_ulAnonymous']/li[2]/a")
WebElement LoginLink;
public void ClickbtnCookieWarning()
{
btnCookieWarning.click();
}
public void ClickLoginLink()
{
LoginLink.click();
}
}
Login Page Object
public class login {
WebDriver driver;
public login(WebDriver driver)
{
this.driver = driver;
}
#FindBy(id="ctl00_ContentPlaceHolderMain_container_container_Block_166_lgn1_UserName")
WebElement UserName;
#FindBy(id="ctl00_ContentPlaceHolderMain_container_container_Block_166_lgn1_Password")
WebElement Password;
#FindBy(id="ctl00_ContentPlaceHolderMain_container_container_Block_166_lgn1_LoginButton")
WebElement btn_LogIn;
#FindBy(id="ctl00_ContentPlaceHolderMain_container_container_Block_166_lgn1_txtAccount")
WebElement Account;
#FindBy(id="ctl00_ContentPlaceHolderMain_container_container_Block_166_lgn1_btnHomeBranch_3")
WebElement btn_Continue;
public void userLogin(String uname, String pass, String acc)
{
UserName.sendKeys(uname);
Password.sendKeys(pass);
btn_LogIn.click();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
Account.sendKeys(acc);
btn_LogIn.click();
btn_Continue.click();
}
}
My Test
public class TC1DefaultDeliveryAddDisplay {
public WebDriver driver;
#BeforeTest(alwaysRun = true)
public void SetUp() {
HomePage HomePage = PageFactory.initElements(driver, HomePage.class);
login loginpage = PageFactory.initElements(driver, login.class);
driver = new FirefoxDriver();
driver.get("http://URL/");
HomePage.ClickbtnCookieWarning();
HomePage.ClickLoginLink();
loginpage.userLogin("aa#yahoo.com", "125", "Test");
}
You're getting NullPointerException because you're using WebDriver instance before initialising.
You need to Initialize WebDriver before using this instance as :-
driver = new FirefoxDriver();
HomePage HomePage = PageFactory.initElements(driver, HomePage.class);
Login loginpage =PageFactory.initElements(driver, login.class);
If you want to use WebDriver as singleton which returns single instance for all your test methods you can follow this answer which is exactly you want.
The problem is in each class you are creating new instance of driver. You just need to create one driver instance in you base class where you do your browser setup. Please refer Page Object Model. Once the Driver instance is created you need to use the same in all your classes. Or else it will throw NullPointerException because driver will not have any reference.

Getting java.lang.NullPointerException when trying to use #FindBy in webDriver

I am getting java.lang.NullPointerException when i am trying to find elements on a webpage using #FindBy annotation.
My Code -
public class pageObject{
WebDriver driver;
#FindBy(id = "email")
WebElement searchBox;
#FindBy(id = "u_0_v")
WebElement submit;
void pageObject(String web){
FirefoxProfile profile = new FirefoxProfile();
profile.setAssumeUntrustedCertificateIssuer(false);
profile.setAcceptUntrustedCertificates(false);
this.driver = new FirefoxDriver(profile);
this.driver.get(web);
this.driver.manage().window().maximize();
this.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
searchBox.click();
searchBox.sendKeys("er");
submit.click();
}
public static void main(String[] args){
new pageObject("https://www.facebook.com/?_rdr=p");
}
}
I got an exception for the above code -
Exception -
Exception in thread "main" java.lang.NullPointerException
at com.Selenium_Practice.pageObject.<init>(pageObject.java:29)
at com.Selenium_Practice.pageObject.main(pageObject.java:35)
I also tried to use
#FindBy(how = HOW.ID , using = "email") and #FindBy(how = HOW.ID , using = "u_0_v")
but again got the same null pointer exception
You first need to init your Elements if you use Selenium's PageFactory and if you want your class to be self-containing*
pageObject(String web){
FirefoxProfile profile = new FirefoxProfile();
profile.setAssumeUntrustedCertificateIssuer(false);
profile.setAcceptUntrustedCertificates(false);
this.driver = new FirefoxDriver(profile);
// You need to put this line in your constructor
PageFactory.initElements(this.driver, this);
// Then follows the rest of your constructor
...
}
* meaning, that you could also init this classes elements outside the class, but I presume you want to do it inside.

Categories

Resources