Handling new window in Internet Explorer using selenium webdriver - java

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/

Related

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

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.

Selenium Testing - Text addition via pop up window is not getting saved

I am trying to automate a website using selenium web driver.
On the website, there is a popup form for creating a gate name with a text field to enter the text. Using selenium I tried to do, the pop up came but the given text was not getting saved. To pop up it's working correctly. Need guides to complete the action.
Methods I used are given below:
Alert alert=driver.switchTo().alert();
driver.switchTo().alert().sendKeys("New Gate");
alert.accept();
System.out.println(alert.getText());
Elements are identified by the Xpath element locator.
I also gave text along with URL, another method that I got from the stack.
If its an alert then right way handle as per your requirement steps should be
switch to and alert
enter text
get entered text
accept
Alert alert=driver.switchTo().alert();
alert.sendKeys("New Gate");
System.out.println(alert.getText());
alert.accept();`
If its a model popup then try to locate popup element and directly used send keys
sample code :
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.your_popup_locator));
driver.findElement(By.you_textbox_locator).sendKeys("expected text");
You need to switch to pop-up first. Then you can access elements in the pop-up.
// save your main window handle
String MainWindow=driver.getWindowHandle();
// Get all window handle
Set<String> handles = driver.getWindowHandles();
Now, for each childHandle in handles,
driver.switchTo().window(childHandle );
// check if your textField present.
// If found, do your actions and break;
// else switch to next window
At last, switch to main window again.
driver.switchTo().window(MainWindow);

How to check the text on an overlay using Selenium?

I'm clicking in on a button on a webpage using Selenium. The button creates a file which can be downloaded now. For this, a overlay is shown in Internet Explorer (yes, I HAVE to use this browser, it's a requirement).
Now I have to check the text on the overlay ("öffnen oder speichern" see my screenshot). I can imagine that it there is a solution using JavaScriptExecutor but I simply couldn't found a solution.
I also tried to find it in innerHTML-without success.
It's not an alert so I can't use Driver.switchTo().alert();
My Code still doesn't contain more than clicking on a button using XPath.
Actions action = new Actions(driver);
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
String exportButtonXPath = generalHelper.getProperty("buttonCSVExportXPath");
WebElement exportButton = driver.findElement(By.xpath(exportButtonXPath));
action.click(exportButton).perform();
Do you have a solution how can test the text on this popup?
Actually, it is not related to the web browser any more. You need to interact with it as a desktop window.
->If you want to click it using selenium, you can locate its coordinates and use click by coordinates using selenium.
->If you want to accept to download it, you can find a capability to accept downloading by default (except IE).
->If you want to check the text value, for sure you've to automate it as desktop not as a web.

How can i download image using selenium webdriver by right clicking the image in java?

I need to save the image by right clicking using selenium and also by "Authenticating the browser with username and password". the link which i am sending through selenium looks like this "http://111.111.2.125/capture".
As I am new this concept, any suggestions will be highly helpful.
For Right Click on Image you can use :
WebElement Image =driver.findElement(By.xpath("//img[#border='0']"));
Actions action= new Actions(driver);
action.contextClick(Image).build().perform();
Select "Save Image As" option
If you see In Image, We can select "Save Image As" option using CONTROL + V from keyboard. To do It In selenium webdriver, We will use bellow given code.
action.sendKeys(Keys.CONTROL, "v").build().perform();
Hope answer by #sForSujit has helped you for 'Right Clicking' on the image.
For Authenticating the browser with username and password, You could consider passing credentials in URL
First, visit the URL with credentials then visit the URL without credentials
Example:
First, access
http://username:password#111.111.2.125/capture
Then, access below one-
http://111.111.2.125/capture

How can i select text from browser screen with help of mouse events in selenium?

I am trying to select text which is already present on the browser.
I want to select that particular text and perform right click operation on it.
However, the page on the browser has disabled right click.
How can I select text in such situation?
Using a normal web browser without Selenium the only workaround that I can think about is to disable javascript to stop the script that prevents you from right clicking. So it should also work with a browser controlled by Selenium Webdriver.
I don't know if it will be OK for you because your website may be relying on javascript for essential features.
However if you don't need Javascript for your Selenium test you can try the following when you launch your driver :
FirefoxProfile p = new FirefoxProfile();
p.setPreference("javascript.enabled", false);
driver = new FirefoxDriver(p);
I assume that you already know how to perform a right click because your question was only about dealing with the problem preventing you from doing this right click. But if not, you can also refer to this answer :
Select an Option from the Right-Click Menu in Selenium Webdriver - Java
Edit:
I'm sorry I really thought you could use Selenium actions to select the text you want but after some tests I didn't manage to perform a click and drag to select a text. The only thing that works for me in Chrome or Firefox is the following. It looks for a <p>which contains some text and then perform a double click to select a word.
driver.get("http://en.wikipedia.org/wiki/Java_(programming_language)");
WebElement text = driver.findElement(By.xpath("//p[contains(text(),'Java is')]"));
Actions select = new Actions(driver);
select.doubleClick(text).build().perform();
However it only highlighs one word in the html element that contains your text, so it's not really convenient.
I've also tried to do Ctrl+F and type the text so that the web browser automatically select it but the browser doesn't do anything when executing :
Actions search = new Actions(driver);
search .sendKeys(Keys.chord(Keys.CONTROL,"+f")).sendKeys("Java is").build().perform();
It seems that Selenium can only send keys events to the html elements and not to the browser (in the case of ctrl+F).
I don't really see a solution for now, let's see if someone else can find a workaround. It's an interesting issue, it would also be useful for me to select a text the way you described
Move to middle of the element
Actions builder = new Actions(webDriverObject);
builder.moveToElement(element).build().perform();
Move to starting of element, click and hold, move to end
Integer width = element.getSize().getWidth();
Actions newBuilder = new Actions(webDriverObject);
newBuilder.moveByOffset(width/2,0).clickAndHold.moveByOffset(width,0).release().build().perform();

Categories

Resources