Using Selenium + Java robot + Scrapbook to download a complete web page - java

I'm trying to download a complete web page (including images, css, scipts, etc.) via Selenium, the java robot class and the ScrapBook firefox extension.
I first tried the CTRL+S method with robot but doesn't worked because that could not press ENTER. So I decided to install ScrapBook on firefox (there's a method which do not displays the download dialog) and tried it with plain robot class, that worked fine. But I need to use it with selenium for rapid testing.
So here is the source code:
package selenium.inexample;
import java.awt.*;
import java.awt.event.KeyEvent;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
public class SelRobot {
public static void main(String[] args) throws AWTException {
// I use my firefox profile for using scrapbook
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("default");
WebDriver driver = new FirefoxDriver(myprofile);
driver.manage().window().maximize();
driver.get("https://www.google.com.");
// This is added only for waiting for the page to load
driver.getPageSource();
//Robot uses the keyboard for interacting with browser and using scrapbook
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_ALT);
for(int i=0; i<5; i++) {
robot.keyPress(KeyEvent.VK_RIGHT);
robot.keyRelease(KeyEvent.VK_RIGHT);
}
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
}
The problem is that scrapbook downloads the page to local\temp\result as a temporal folder, then it deletes all subfolders and files when the session expires. I need it to be downloaded as a permament page inside the firefox profile folder.
The robot uses scrapbook directly once the page loads.
I'm using my firefox profile in the code because it has the scrapbook extension installed.
I want to point out that the plain robot (with scrapbook) works fine, but the problem is when I use Selenium (is there some config I need to do?).
Also, please note the code is just a simple example. If it works, I'll use something similar in production.

I found the solution!!!
The problem was that Scrapbook always downloaded the page to the profile folder. When I used Selenium, a temporary Profile was always created, even If I created a new one or used an already existing one.
So, I just configured scrapbook to download the page to any other folder diferent to the profile.
Just pressed Alt+k, then Tools > Options > Organize. There I just changed the folder of the downloads. That way, selenium doesn't deletes the page because it's not part of it's temp profile data.

Related

How to trigger a click on a chrome extension button?

I'm building an automated test suite using Selenium Web Driver. At a certain point I must test how the page works by having a Chrome extension turn on or off. Think of it as you would want to click on the Adblock extension and then click disable for this site. Then, turn it on again.
I searched all over the Internet and there is no way to implement this using just Selenium. Do you know how could I perform such an action? (from Java ideally)
One possible solution is to go with Chrome options and manage the extensions set to the WebDriver. Quick example:
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
If you want to turn those On and OFF in a single test, you can spawn two separate drivers and compare the results, since I'm not sure that session reuse will do the job in this case.
Below is the solution is in Python with pyautogui (I believe it's similar to autoit in java - so you can extend the same solution for java also).
Pre-Condition:
save the extension image in the project folder (I saved it under "autogui_ref_snaps" folder in my example with "capture_full_screenshot.png" name
Python:
Imports needed
from selenium import webdriver
from selenium.webdriver import ChromeOptions
import pyautogui #<== need this to click on extension
Script:
options = ChromeOptions()
options.add_argument("--load-extension=" + r"C:\Users\supputuri\AppData\Local\Google\Chrome\User Data\Default\Extensions\fdpohaocaechififmbbbbbknoalclacl\5.1_0") #<== loading unpacked extension
driver = webdriver.Chrome(
executable_path=os.path.join(chrome_options=options)
url = "https://google.com/"
driver.get(url)
# get the extension box
extn = pyautogui.locateOnScreen(os.path.join(GenericMethods.get_full_path_to_folder('autogui_ref_snaps') + "/capture_full_screenshot.png"))
# click on extension
pyautogui.click(x=extn[0],y=extn[1],clicks=1,interval=0.0,button="left")
If you are loading an extension and it's not available in incognito mode then follow my answer in here to enable it.
Can use sikuli(GUI Automation tool) to click on browser addon.
Imports needed:
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
Script:
Pattern addon=new Pattern("D:\\My Files\\Addon.jpg"); //image of the addon must be given as a pattern for identifying that on the browser/webpage
Screen s=new Screen();
s.hover(addon);
s.click(addon);
If you want to click the extension icon on the right side of the chrome and it is the extension that will be opened during the opening of the page or after clicking action
you can use this
public void openBrowserExtension(){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.postMessage('clicked_browser_action', '*')");
}

Selenium: -browser.helperApps.neverAsk.openFile and savetodisk is not working

I have a critical issue over here.
Please find my scenario below:
login
click on a link
after the click, a new tab opens
I have switched the focus to the new opened tab with the following code
ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(newTab.get(1));
The issue is that when I try to click on an excel download link on the newly opened tab, the "Open with" popup is appearing and my automation fails. Even after adding the following preference
firefoxProfile.setPreference("browser.helperApps.neverAsk.openFile",
"text/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"text/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
Please, can you suggest a feasible solution as soon as possible?
Thank you
Please manually set these preferences in your firefox's(about:config) section, visit the application, click the link and see if the file gets downloaded without any prompt. This will help you to identify the issues with automation.
I tried setting these preferences in my firefox, but it still prompts download window. I can download without prompt only after checking "Do this automatically for files like this from now on" which updates mimeTypes.rdf file in the profile directory. So to make this work through automation, you may need to bundle a custom firefox profile that includes a mimeTypes.rdf with your TestSuite.
Here's the code to create FirefoxProfile from a given profile directory:
FirefoxProfile profile = new FirefoxProfile(new File("<PATH_TO_FIREFOX_PROFILE_DIRECTORY_THAT_WORKS_WHEN_TESTED_MANUALLY>");
WebDriver driver = new FirefoxDriver(profile);

File upload selenium using sendKeys

My situation is that I am automating testing file upload feature. Now the automation ci works on another machine/box and the browser is opened in another machine(s) for the automation testing. I am able to package (add the test input file to be uploaded) in the jar. But the jar is in another another machine as mentioned above and the browser is in another machine(s). Since the browser machine is not fixed and picked up at automation runtime, so how can I have the input file which I need to upload available on the machine where browser is running.
I tried to copy the file after extracting it from the jar, but obviously it doesn't get copied in the browser machine(s), from where it is uploaded.
Is it even possible to have that file available in the browser machine(s)?
One thing you can do is putting the uploading file in to a shared folder which can be accessed from all the running machines. And give the file location from the shared folder.
You can give the sendKeys command to the upload field like below
upload_textfield.sendKeys("\\shared_Folder\upload.txt")
static WebDriver driver;
public static void main(String[] args) throws InterruptedException, FindFailed {
System.setProperty("webdriver.gecko.driver", "E:\\doftware\\geckodriver-v0.10.0-win64\\geckodriver.exe");
driver =new FirefoxDriver();
driver.get("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1");
driver.findElement(By.id("Email")).sendKeys("emailaddress");
driver.findElement(By.id("next")).click();
Thread.sleep(500);
driver.findElement(By.id("Passwd")).sendKeys("Password");
driver.findElement(By.id("signIn")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//div[#class='T-I J-J5-Ji T-I-KE L3']")).click();
Thread.sleep(500);
driver.findElement(By.xpath("//div[#class='a1 aaA aMZ']")).click();
org.sikuli.script.Pattern open= new org.sikuli.script.Pattern("C:\\Users\\narendra\\Desktop\\test\\filename.PNG");
org.sikuli.script.Pattern open1= new org.sikuli.script.Pattern("C:\\Users\\narendra\\Desktop\\test\\open.PNG");
org.sikuli.script.Screen scr= new org.sikuli.script.Screen();
scr.setAutoWaitTimeout(30);
scr.type(open, "C:\\Users\\narendra\\Desktop\\test\\searchButton");
scr.click(open1);

Configuration of Selenium 2 (WebDriver), using IE and upload file with WebDriver

How do I configure Selenium WebDriver? I have automated test cases using Selenium with Java. Now I need to automate upload and download of a file using WebDriver. I had added webdriver-common-0.9.7376.jar. I like to use Internet Explorer. How can I do that?
I'm just declaring variable and using driver
private static WebDriver driver;
driver.findElement(By.id(upload)).sendKeys("file to be upload");
Is this correct?
Ques. 1: How to configure WebDriver?
Ans: There are 2 ways: 1) Adding "selenium-server-standalone-2.29.0.jar" only
OR,
2) Adding "selenium-java-2.29.0.jar" and all the jars located on "selenium-java-2.29.0\selenium-2.29.0\libs" folder
You can download "selenium-server-2.29.0.zip" and "selenium-java-2.29.0.zip" from http://code.google.com/p/selenium/downloads/detail?name=selenium-server-2.29.0.zip and http://code.google.com/p/selenium/downloads/detail?name=selenium-java-2.29.0.zip respectively.
Extract them and you could get corresponding jar files to add.
Ques. 2: How to instantiate IE and how to upload file?
Ans: The java code as below:
File file = new File("C:\\Program Files\\Internet Explorer\\iexplore.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
driver.findElement(By.id("upload")).sendKeys("file to be upload");
If "File file = new File("C:\Program Files\Internet Explorer\iexplore.exe");" doesn't work download "IEDriverServer" and replace that line with below:
File file = new File("E:\\Ripon\\IEDriverServer_Win32_2.29.1\\IEDriverServer.exe");
[Note: You can download "IEDriverServer" from http://code.google.com/p/selenium/downloads/list ]
You need to add all jar after downloading selenium-java 2.25 0r any version. First add all jar then all all lib folder jar.
selenium-java-2.25.0.jar
selenium-java-2.25.0-srcs.jar and then all lib jar (Don't forget to add all lib folder jar)
Without instantiate driver for your browser, it won't open a browser window to do the upload/download operation. If you're using IE you've to write driver = new InternetExplorerDriver();
Instead of the old and outdated webdriver-common package, you probably need the newest selenium-java from http://code.google.com/p/selenium/downloads/list.
If you'll ever also need running Selenium RC locally, or Remote WebDriver ot Selenium Grid, you'll need the selenium-server package there (if you don't yet know what these are, just take selenium-java).
In both cases, for running InternetExplorerDriver, you'll also need the IEDriverServer from the page mentioned above. It's up to you whether to use the 32 or 64 bit version.
You can find an example of setting it up here in the documentation. If you dig around a bit, you'll find many more useful information in that documentation.
For example, for Internet explorer, you'll do:
System.setProperty("webdriver.ie.driver", "C:\\path\\to\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
// your testing code
driver.quit();
Your method of uploading a file is correct.
And as of now (Selenium v2.29.0), you can't download files via any WebDriver. If you really want to do so, you'll have to find another way.

What library do I need to use "File" type with selenium?

I want to use Selenium to control Firefox, but I want to control a custom profile of Firefox.
I found this:
File profileDirectory = new File(profileDirectory);
FirefoxProfile profile = new FirefoxProfile(profileDirectory);
WebDriver webDriver = new FirefoxDriver(profile);
But the File symbol is not found and I don't know what library contains it.
Give a man a fish, Teach a man to fish...
If you do a google search for "java File" It will take you to this link:
http://docs.oracle.com/javase/6/docs/api/java/io/File.html
The very top of that page displays a miniature tree which tells you that in Java version 1.4.2, the symbol: "File" is an Object that is extended by java.io.File
java.io.File is part of the standard Java library.
The following should allow your code to compile:
import java.io.File;

Categories

Resources