Unable to do file upload functionality in Jenkins using Robot & AutoIT scripts - selenium

Here it is the AutoIT script
ControlFocus("File Upload","","Edit1")
ControlSetText("File Upload","","Edit1", "file path")
ControlClick("File Upload","","Button1")
And Robot script is:
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
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);
I just tried with the above scripts in jenkins execution but it is not working.
Can anyone please suggest me with the proper execution script.

If your using Selenium as you've tagged the question, the proper way to upload a file is using sendKeys, here's an example:
string File = "SomeTextFile.txt";
string FilePath = #"C:\Whatever\" + File;
driver.get("http://the-internet.herokuapp.com/upload");
driver.findElement(By.id("file-upload")).sendKeys(FilePath);
driver.findElement(By.id("file-submit")).click();
Your script for Jenkins could be failing for multiple reasons you have to watch it when it happens. Could be your clicking a wrong button, or you don't have an active desktop in your slave etc...

Related

Firefox File upload through Selenium scripts "Unable to determine type from: H. Last 1 characters read: H"

While trying to upload file in selenium scripts am getting the error "Unable to determine type from: H. Last 1 characters read: H" on execution through Firefox. But in chrome it works fine.
Hence i tried with Robo class
StringSelection stringSelection = new StringSelection(filePath);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
waitABit(4000);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
waitABit(6000);
This is not supporting on execution through Browser Stack.
Can anyone help on providing a sample code that support file upload in all browsers and all platforms?

How to launch my application through winappdriver?

I am new to the WinAppDriver Windows Based Automation. Kindly help me to launch my windows application through winappdriver.
String applicationPath = System.getProperty("user.dir")+"/Data/TestData/StudioSetup.exe";
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("app", applicationPath);
WindowsDriver driv = new WindowsDriver(new URL("http://127.0.0.1:4723"), capabilities);
It launches my application, but it takes long time to open the window. In the meanwhile, it throws the below exception in the 4th line: -
org.openqa.selenium.SessionNotCreatedException: Unable to create a new remote session. Please check the server log for more details. Original error: Failed to locate opened application window with appId: C:\Users\Peenu\git\Uptime/Data/TestData/StudioSetup.exe, and processId: 7208 (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 7.17 seconds
This worked for me:
AppiumOptions appOptions = new AppiumOptions();
appOptions.AddAdditionalCapability("app", "PATH TO YOUR EXE");
appOptions.AddAdditionalCapability("deviceName", "WindowsPC");
appOptions.SetLoggingPreference(OpenQA.Selenium.LogType.Server, OpenQA.Selenium.LogLevel.All); //optional
WindowsDriver<WindowsElement> driv = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appOptions);
See if this works
Process.Start(#"<WinappDriver.exe path>");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability("deviceName", "WindowsPC");
capabilities.SetCapability("app", #"<Path to application.exe>");
BasePage.WindowsDriver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), capabilities);
Thread.Sleep(10000); //Uncomment if required
BasePage.WindowsDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
The application path is incorrect. Copy the path of your exe file using these steps:
Go to the application folder
Press the SHIFT key and right click the application icon
Select "Copy as path" from context menu
Now go back to your code and paste this value there.
Put an # before the string
For example, the path to notepad looks like the following.
#"C:\Windows\System32\notepad.exe"

How to run Robot class commands with selenium before browser page load

I am trying to automate a page which has an Active directory authentication. I am using Robot class to pass the user/Password which is not working.
I tried debugging and it seems like the Robot class commands are not working because the page is still loading in the background when the AD Authentication window pops up.
Same code works fine when I replace the URL with Google.com portal.
Request your help, Thanks.
Here's the code...
driver.get("https://www.mypageurl.com");
//driver.get("https://www.google.com");
//none of the below line of codes are not executed for mypageurl but works for Google
Thread.sleep(3000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
//Code to enter username
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
//Code to enter password
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Try JavaScript executor to wait for page to load.Please see below link hope this help you.
https://www.testingexcellence.com/webdriver-wait-page-load-example-java/

Selenium : New tab is not getting opened on same browser in Chrome

I'm trying to open a new tab in same browser but it doesn't seem to work. I'm using Chrome Version 58.0.3029.110 (64-bit) and Selenium 3.0.0.
I used the below code:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t");
try using JavascriptExecutor as below:
((JavascriptExecutor) driver).executeScript("window.open('https://www.google.com');");
You can also use Robot class with Selenium Webdriver to open a new tab. We need to follow the below three steps-
Simulate pressing of Ctrl+t keys of keyboard using Robot class.
Switch to the new tab in selenium using driver.switchTo() command.
Open the desired link on new tab.
Code snippet-
//Launch the first URL
driver.get("http://www.google.com");
//Use robot class to press Ctrl+t keys
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_T);
//Switch focus to new tab
ArrayList<String> tabs = new ArrayList<String (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
//Launch URL in the new tab
driver.get("http://google.com");
Source: Code snippet from Open a new tab in Selenium - ArtOfTesting

Upload file using webdriver

I have tried to upload a photo using the code
driver.findElement(By.xpath("xpath")).sendKeys("C:\\Users\\path\\ben.jpg");
But the image is not getting uploaded.
The html of the upload button is
<button id="upfile1" class="buttonclass" style="cursor: pointer" type="button"> Choose Photo</button>
Is there any other way to upload image. I have tried using WebElement also. I need a solution in JAVA.
I was able to do it using
driver.findElement(By.id("upfile1")).click();
Thread.sleep(2000);
StringSelection ss = new StringSelection("C:\\Users\\logo1.jpg");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
But is there any other simple methods to achieve the same other than using robot?
Add System.Windows.Forms reference and try the below code.
driver.findElement(By.Id("upfile1")).Click();
System.Windows.Forms.SendKeys.SendWait("C:\\Users\\path\\ben.jpg");
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
The pop-up which appears is a windows one.It should be handled using AutoIT.
If you are using RemoteWebDriver, then you need to use LocalFileDector to upload the file to the remote selenium server first. And then use the remote path to upload from the remote selenium server.
driver.setFileDetector(new LocalFileDetector());
driver.findElement(By.xpath("xpath")).sendKeys("C:\\Users\\path\\ben.jpg");