Reputation: 5671
I have a Selenium WebDriver
based testcase, which pauses during execution. It should upload thousands of files to a website. When it chooses the file to upload it sometimes doesn't click ok, but waits for manual interaction. In most cases it is working perfectly.
I use StringSelection
to copy and paste file source to input field.
StringSelection cp = new StringSelection(fileSource);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(cp, null);
Upvotes: 0
Views: 78
Reputation: 294
Below code working fine for the similar scenario in our environment.
StringSelection cp = new StringSelection(fileSource);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(cp, null);
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
We can also use AutoIT to perform this type actions.
Please find AutoIT code to handle this case.
Download AutoIT ,write this code in AutoIT name it as 'Loadfromdisk' and compile. .exe will be generated, please place exe somwhere in your local drive(ex: E:\Loadfromdisk.exe)
AutoItSetOption("WinTitleMatchMode","2") ;
$title=WinGetTitle("[CLASS:DirectUIHWND; INSTANCE:2]")
WinActivate($title)
WinWaitActive($title)
If WinExists($title) Then
WinFlash($title,"", 4, 500) ;Just to Flash the window
EndIf
Sleep(1000)
ControlSetText($title, "", "Edit1", fileSource)
Sleep(1000)
ControlClick($title,"","Button1")
Exit
Load from disk Selenium Java code, this will load the file placed at 'filesource' path mentioned in AutoIT code into web application
String strAutoIT = "E:\\Loadfromdisk.exe";
Thread.sleep(3000);
String[] astrArg = null;
astrArg=new String[]{strAutoIT};
Runtime.getRuntime().exec(astrArg);
Please see whether this helps to run your testcase.
Upvotes: 0
Reputation: 346
For behavior "When it chooses the file to upload it sometimes doesn't click ok, but waits for manual interaction. In most cases it is working perfectly." I prefer use failed retry count. Every step with click should be wrapped up on the test and if test result=failed - retry test some times(3 or 5). JUnit have good mechanizm for that:
@RunWith(Runner.class)
public abstract class AbstractTest extends LibTest {
@Rule
public JUnitRetry retry = new JUnitRetry(3);
}
public class Test extends AbstractTest
@Test
public void testCp(String fileSource){
StringSelection cp = new StringSelection(fileSource);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(cp, null);
}
}
Upvotes: 1
Reputation: 287
I think your test could be running to quickly? If this is the case, then you could potentially use WebDriverWait?? WebDriverWait could be used to wait for the 'OK' element to be visible prior to clicking and therefore proceeding.
I might be wrong, but I can't really tell what the issue is without the rest of the code.
Personally, I use the following method which I can then call
public void waitForElementToBeVisible(String cssSelector) throws Throwable {
try {
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.or(
ExpectedConditions.visibilityOfElementLocated(By.cssSelector(cssSelector))
));
} catch (Exception e) {
System.out.println("Timeout exceeded");
closeDriver();
}
}
Upvotes: 1