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<mpl=default<mplcache=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);
Related
I am using Java and Selenium to automate my tests.
One of my tests needs to upload an image from the computer.
I used Robot object to recognize the upload window and set the path of the file.
When running the test via IntelliJ everything is OK, as the browser comes to front, including the upload window.
When I run the test through Jenkins, the browser and the upload window remain at the back and fail to upload the file.
I tried several ways to bring the windows to the front and they all work only when running directly via IntelliJ, but it's still a problem with Jenkins.
driver.switchTo().window(driver.getWindowHandle());
or
((JavascriptExecutor) driver).executeScript("window.focus();");
Any idea how to handle this uploading window?
public static void uploadFile(String filePath) throws AWTException
{
//Copy the file path to the clipboard
SystemOps.setClipboardData(filePath);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
sleepUninterruptibly(3000);
}
enter image description here
Well, this is what I have done to solve it:
I installed AutoIt FULL INSTALLATION from their website:
https://www.autoitscript.com/site/autoit/downloads/
Then installed AutoIt editor (same page) but redirects here:
https://www.autoitscript.com/site/autoit-script-editor/downloads/
In the past they had a recorder that enabled recording AutoIt scripts, but no more.
In order to download the recorder you can download the zip from here:
https://www.autoitscript.com/autoit3/files/archive/autoit/autoit-v3.3.14.0.zip
Extract it, and copy the 'Au3Record' folder under 'Extras' folder and put it in the existing 'Extras' folder you already have.
This recorder exe file enables you to record steps you do on window-based apps and convert them to scripts.
My approach was to create a function that would copy the file path and paste it to the upload window, rather then type it, as I didn't want / know how to modify the exe file every time, and it works for me this way.
Open a text file and paste this code:
_WinWaitActivate("Open","")
Send("{ALTDOWN}n{ALTUP}{CTRLDOWN}v{CTRLUP}")
MouseClick("left",514,441,1)
(The Alt+N is just to make sure we are focusing on the file input field)
Save the file as uploadFile.au3 somewhere in your project.
Convert the file to an .exe file:
Open 'Aut2exe' under Aut2exe folder. Convert the .au3 file you created to .exe
Place the file in your project.
The code:
public static void uploadFile(String filePath) throws IOException
{
//Copy the file path to the clipboard
SystemOps.setClipboardData(filePath);
//Execute the exe + Location of folder
Runtime.getRuntime().exec("./AutoItExeFiles/uploadFile.exe");
Uninterruptibles.sleepUninterruptibly(milliseconds, TimeUnit.MILLISECONDS);
}
/**
* Sets any parameter string to the system's clipboard.
* #param string the string to copy to the clipboard
*/
public static void setClipboardData(String string)
{
//StringSelection is a class that can be used for copy and paste operations.
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
I'm trying to create a jar that enables people to launch web browser drivers through this library.
So the idea is to have one project "ToBecomeJar" have chromedriver (does not have a file extension) as an item in a "drivers" folder and using it in its code as:
private WebDriver getDriver(){
System.setProperty(CHROME_DRIVER_PROPERTY, "drivers/chromedriver");
driver =new ChromeDriver();
return driver;
}
The issue here is that when I turn this project into a library, of course the "PathToDriver" will be taken as an absolute path when used, leading to my new project needing path structure exactly the same as the library with the driver there.
Is there any way to make this relative?
I've tried working with a resource folder and calling the resource with .getResource but I really can't manage to make it work. When looking into this people mention that it should become .getResourceStream as it becomes something other then a file, but that doesn't work for me as it's not a text file I'm trying to use.
Try to use 'user.dir' which is an environment var of the JVM.
It can be used to construct the path string of your driver executable:
private WebDriver getDriver(){
System.setProperty(CHROME_DRIVER_PROPERTY, System.getProperty("user.dir") + "/drivers/chromedriver");
driver = new ChromeDriver();
return driver;
}
You can extract the needed resources from jar to a temporary folder during the startup of your app and then point to that folder for the proper drivers.
After passing all test cases( aka scenarios in Cucumber), I'm trying to invoke a method using #After hook Cucumber capability in order to open a report file in Chrome browser. The idea is to display a report file using Selenium WebDriver after all tests are passed. When the Cucumber invokes this #After hook method, Selenium starts, gets this path, but the Chrome doesn't open the report file. Chrome says "Your file was not found It may have been moved or deleted. ERR_FILE_NOT_FOUND". But if then I manually click on reload button, the file opens successfully.
Please help me when you are familiar with Cucumber and Selenium.
Thank you in advance.
P.S. Yes, I also tried to replace '\' by '/'
When Selenium tries to access that report html file:
chrome screenshot
My Java code is:
#After
public void afterScenarios(){
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Testing\\BrowserDrivers\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window();
System.out.println("Opening the report...");
driver.get("C:\\Users\\target\\cucumber-report\\business-acceptance-test1\\index.html");
}
The reporting process is not fully complete when the After hook is executing. Try placing the selenium code in a JVM shutdown hook. This will make sure everything is completed.
Runtime.getRuntime().addShutdownHook(new Thread()......);
Refer to this - https://www.geeksforgeeks.org/jvm-shutdown-hook-java/
I am developing a framework using selenium webdriver in Java. I have two maven projects. One is for the framework and other is for the test project.
In order to launch launch ChromeDriver, I need to set the system property with the path of the chromedriver.exe file. I am doing this in DriverFactory.java which is in /src/main/java of the Framework Project. Now, if I place the exe files in src/main/resources/drivers, Java complains that the file is not found. The code is :
private static String chromeDriverLocation = "drivers/chromedriver.exe";
File cDriver = new File(DriverFactory.class.getResource(chromeDriverLocation).getFile());
// Is it executable
if (!cDriver.canExecute()) {
cDriver.setExecutable(true);
}
System.setProperty("webdriver.chrome.driver", DriverFactory.class.getResource(chromeDriverLocation).getFile());
I tried placing this file in src/main/resources/drivers in the Test Project and modified the code like :
private static String chromeDriverLocation = System.getProperty("user.dir")+ "\\classes\\drivers\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverLocation);
Though the above piece of code works in local when executed through Jenkins(Maven), I am not sure if this is the best way. Can someone suggest other good ways of achieving this ?
As an option, you may place the chromedriver.exe file into a directory added to PATH. In this case running the ChromeDriver will be as simple as creating an instance of new ChromeDriver() without dealing with system properties.
If you still would like to store chromedriver.exe under Resources, you may get it as steam, save to a file and set the "webdriver.chrome.driver" property:
InputStream in = getClass().getResourceAsStream("chromedriver.exe");
OutputStream out = new FileOutputStream("chromedriver.exe");
IOUtils.copy(in, out);
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.