Selenium click() doesn't work on new opened window? - java

after logging in with selenium to www.playok.com/en/spades/, the webiste opens a new window which has the same url. but after that, no click on no element works:
System.setProperty("webdriver.firefox.marionette", "some directory");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.playok.com/en/spades/");
driver.findElement(By.cssSelector("button.ckbut.but0")).click();
driver.findElement(By.cssSelector("button.lbpbg.ttup")).click();
WebElement username = driver.findElement(By.cssSelector("input#id"));
username.sendKeys("some username");
WebElement password = driver.findElement(By.name("pw"));
password.sendKeys("some password");
driver.findElement(By.cssSelector("input.bxpad.ttup")).click();//here a new page opens at the same window, which has a start button:
driver.findElement(By.cssSelector("button.lbprm.ttup")).click();//here a new window opens which goes to the main game page and the url is still the same
//but the following click doesnt work; actually no button works, doesnt matter if here a sleep() gets used, click won't work anyway.
driver.findElement(By.cssSelector("button.butsys.minwd")).click();
almost all the buttons were tested; also a sleep() has been used there, but no difference; still no button works.
but there is no other way to access to the website without logging in, and when logging in, this problem accurs.

Related

How to check if a popup window is open or not using Selenium WebDriver in Java

I'm trying to check whether the popup window I want to open is opened or not.
I have checked some question answers like
How would you check if a popup window exists using selenium webdriver?
But, nothings helped to solve the problem.
Here, first I open the login window by clicking the login button.
driver.findElement(By.xpath("//a[#id='login_btn']")).click(); // Click Login Button
I even tried getPageSource() but, it seems not working.
Any kind of help would be appreciated.
Thanks in advance. :)
If it's a native, browser alert (= a popup) you can do the following:
try{
driver.switchTo().alert();
// If it reaches here, it found a popup
} catch(NoALertPresentException e){}
What it looks like you're actually dealing with is an iframe which you can do the following after getting the attribute value of the "iframe" attribute:
driver.switchTo.frame("ValueOfIframe");
// Treat as normal webpage. Now in iframe scope
driver.switchTo.defaultContent(); // To return back to normal page scope
String mwh=driver.getWindowHandle();
Now try to open the popup window by performing some action:
driver.findElement(By.xpath("")).click();
Set s=driver.getWindowHandles(); //this method will gives you the handles of all opened windows
Iterator ite=s.iterator();
while(ite.hasNext())
{
String popupHandle=ite.next().toString();
if(!popupHandle.contains(mwh))
{
driver.switchTo().window(popupHandle);
/**/here you can perform operation in pop-up window**
//After finished your operation in pop-up just select the main window again
driver.switchTo().window(mwh);
}
}

Methods used to verify a "cancel" button in web application works

I am working on test cases for a web application. The current job requires me to confirm that when you press the logout button, the prompt comes up, you can click the cancel button on the prompt, and the prompt will close.
I am wondering about verify and assertion methods I could use to confirm this functionality works. The cancel button does nothing else but close the pop up prompt. What would you guys use?
Here's some code:
Actions actions = new Actions(driver);
WebElement logout = driver.findElement(By.xpath(".//*[#id='flow']/div[1]/div/div[7]/div/div[3]/div[4]/div/div/div/div/div/div/div/div[1]/div/img"));
actions.moveToElement(logout).build().perform();
WebElement logoutHover = driver.findElement(By.xpath(".//*[#id='flow']/div[1]/div/div[7]/div/div[3]/div[4]/div/div/div/div/div/div/div/div[3]/div/img"));
logoutHover.click();
WebElement logoutPushed = driver.findElement(By.xpath(".//*[#id='flow']/div[1]/div/div[7]/div/div[3]/div[4]/div/div/div/div/div/div/div/div[4]/div/img"));
logoutPushed.click();
WebElement cancel = driver.findElement(By.xpath("html/body/div[3]/div[4]/div/div/div[6]/div[2]/div/div[2]/div/div[3]/div/div/div/div[5]/div"));
actions.moveToElement(cancel).build().perform();
WebElement cancel2 = driver.findElement(By.xpath("html/body/div[3]/div[4]/div/div/div[6]/div[2]/div/div[2]/div/div[3]/div/div/div/div[5]/div"));
cancel2.click();
WebElement pageText = driver.findElement(By.xpath("html/body/div[3]/div[1]/div/div[3]/div/div/div/div/div/div/div/div/div[2]/div[2]/div/div/div/div/div[2]/div"));
Assert.assertTrue("Text not found!", pageText.contains("PRODUCT LIST"));
This assert method does not work. My initial idea was that if you hit the cancel button, I can assert that the user is still on the same page (my code for this does not work). Would it be a smarter choice to assert that the prompt is not present anymore? If so, how would I go about doing that?
I believe the correct way to do it would be to confirm that the logout prompt is no longer up and that you are still logged in. I don't know what your site looks like but for the logout prompt, find an element unique to that popup (maybe the OK or cancel button, hopefully something with an ID). Detect that it's no longer visible and then confirm you are still logged in by some means... look for a user name or ???
// click the logout button
// click the cancel button
List<WebElement> button = driver.findElements(By.id("sampleId")); // a button on the confirm popup
if (button.isEmpty())
{
// the logout confirmation popup is not visible
// verify that you are still logged in... maybe look for a user name or ???
}
else
{
// log a failure here because you couldn't cancel the logout popup
}

How to Handle POPUP browser in Selenium2(Webdriver)?

I'm using Selenium2 (Webdriver).
Well, I have a Problem.
I have a A Browser.
When I click one button in A Browser, Another popup browser(B) is open.
And, The process what I have to do in B browser is done, B browser wasn't close.
But, this is not my problem. B browser is originally Designed this way.
my problem is ..
After B browser's process is finished, when I tried to find out C element in A browser, So B browser wans't closeed, that I can't find out C element. because Selenium tried to find out C element in B browser.
I just want...
When I do something in Browser B, Browser B has a Handle.
and the Process in Browser is done, the handle must moving in A Browser.
How can I? Please Help me.
Here is the sample code for switching between the Windows:
With windows handle we can switch between windows.
WebDriver driver=new FirefoxDriver();
//First navigating to Yahoo site
driver.get("http://www.Yahoo.com");
//Capturing the window handle
String strMainWindowHandle=driver.getWindowHandle();
//For better understanding printing the page title
System.out.println(driver.getTitle());
//Now opening a new window
driver.findElement(By.xpath("//body")).sendKeys(Keys.CONTROL+"n");
Set<String> winHandles=driver.getWindowHandles();
for(String handle:winHandles)
driver.switchTo().window(handle);
//Navigating to Google site with new window i.e new window handle
driver.get("http://www.google.com");
//For better understanding printing the page title
System.out.println(driver.getTitle());
//Switching back control to main Window which captured earlier
driver.switchTo().window(strMainWindowHandle);
System.out.println(driver.getTitle());
Once you come back to your main window handle you can perform Click or whatever u want.

Handling new window in Internet Explorer using selenium webdriver

I am using selenium webdriver to automate a scenario where in clicking on a download image opens a new window. This new window will have the "Save As" dialog box.
As soon as I click on the image, new window opens up but is closed immediately without displaying the "Save as" Dialog box.
My scenario is to make sure the file is present and read the file name by clicking on the save as button.
My code:
WebElement e1= driver.findElement(By.id("id of image"));
e1.click();
Set<String> set = driver.getWindowHandles();
List<String> handles = new ArrayList<String>(set);
driver.switchTo().window(handles.get(1)); // switch to file download dialog box
Based on my exp, Seleniun file download test is a pain to automation engineer. Instead of doing step-by-step download, we could use HttpURLConnection, Apache HttpComponents (or maybe just a file get through URL) for the link specified and assert a 200 OK response.
For more information, you can refer to following link for tools to handle this case
https://github.com/Ardesco/Ebselen/blob/master/ebselen-core/src/main/java/com/lazerycode/ebselen/customhandlers/FileDownloader.java
http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/

Not able to switch to pop-up window and find any elements in pop-up in Webdriver using Java

I have an application in which i tried clicking on a button and in return it will pop-up a window for filling a new user form. This is not really like a pop-up window, because it has some input fields as well as "save " and "cancel" button. It looks similar to pop-up window in facebook.
Here the code i tried with
Set beforePopup = driver.getWindowHandles();
driver.findElement(By.xpath("/html/body/div/div/div/div[3]/div/div[2]/div[2]/table/tbody/tr/td[3]/table/tbody/tr/td[2]/em/button")).click();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
Set afterPopup = driver.getWindowHandles();
afterPopup.removeAll(beforePopup);
if(afterPopup.size() == 1) {
driver.switchTo().window((String)afterPopup.toArray()[0]);
}
//driver.switchTo().window("Add New User");
//selenium.type("userDetailsBean.firstName","alan1");
//WebElement btnStartQueryInside = driver.findElement(By.xpath("//html/body/div[14]/div/div/div/div/span"));
//btnStartQueryInside.getText();
System.out.println(driver.getTitle());
WebElement firstName = driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div/div/form/div/div/input"));
firstName.sendKeys("alan1");
//driver.switchTo().alert();
//driver.findElement(By.xpath("//html/body/div[14]/div/div/div/div")).getText();
//WebElement firstName=driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div/div/form/div/div/input"));
//firstName.sendKeys("alan1");
/*WebElement lastName=driver.findElement(By.id("userDetailsBean.lastName"));
lastName.sendKeys("harper");
WebElement emailadd=driver.findElement(By.id("userDetailsBean.userEmail"));
emailadd.sendKeys("alan1#derik.com");
WebElement username=driver.findElement(By.id("userDetailsBean.userName"));
username.sendKeys("xalan1");
WebElement password=driver.findElement(By.id("adminPassword"));
password.sendKeys("Derik123");
WebElement repassword=driver.findElement(By.id("adminPassword2"));
repassword.sendKeys("Derik123");
driver.findElement(By.xpath("//html/body/div[2]/div/div/div/div/div[2]/div/div/table/tbody/tr/td/table/tbody/tr/td[2]/em/button")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);*/
Please note that in code I commented some input field filling, because I thought first I will make it working just for 1st field. Please help me how to proceed.
the problem is after clicking the pop-up button, I'm not sure if the control switches to pop-up window or not. the gettitle after pop-up gives the main window title. and it is not able to find the first input field using the xpath or id.
The term window in Selenium means the actual browser window, not frames or just divs. Therefore, your logic is wrong, getWindowHandles() and driver.switchTo().window("Add New User") are not what you are after.
What you need is driver.switchTo().frame().
driver.switchTo().defaultContent(); // optional, use only if driver is already in an iframe
WebElement editUserForm = driver.findElement(By.cssSelector("iframe[src*='editUserForm']"));
// there are other overloads (by frame name, index, id) and locators can be used here.
driver.switchTo().frame(editUserForm);
// make sure your locator here is correct
WebElement lastName = driver.findElement(By.id("userDetailsBean.lastName")); // I doubt this is correct
// from your screenshot, I'd suggest By.cssSelector("[id*='userDetailsBean.lastName'] input")
lastName.sendKeys("harper");
Also just a kindly heads up, your code smells.
Your implicitlyWait wait usage seems wrong, please read the
documentation and the post.
Where did you get selenium.type("userDetailsBean.firstName","alan1");? Did you just copy the line from somewhere else? This looks like Selenium RC to me.
Why driver.switchTo().alert()? Have you read the documentation on what's this for?
Please don't use absolute xpath.
Try use page object if you haven't done that in your real project. (It's fine if you just want to illustrate the problem here)

Categories

Resources