我创建了一个方法(在搜索此站点后)将文件路径输入到windows文件管理器中。这很有效,但我想扩展我的测试用例,循环遍历一组文件位置,并将测试文件传递给web应用程序,以测试它们是否得到了适当的处理。
我遇到了一个问题,在第二次传递(我有两个测试文件)时,windows处理程序方法不想输入文件名。有人能扫一扫我的代码给我指出正确的方向吗?
下面是windows文件管理器的方法:
public class WindowsFileSystemHandler {
String filepath;
public void enterfilepath(String pfilepath) throws Exception{
filepath = pfilepath;
StringSelection ss = new StringSelection(filepath);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
Robot robot = new Robot();
//robot.keyPress(KeyEvent.VK_ENTER);
//robot.keyRelease(KeyEvent.VK_ENTER);
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);
Thread.sleep(2000);
}
}
下面是测试结果:
WindowsFileSystemHandler file = new WindowsFileSystemHandler();
String[] fileloc = {"d:\\JW\\testfiles\\scribbles.txt", "d:\\JW\\testfiles\\earth_large_file_size.jpg"} ;
for(int i =0 ; i<2; i++){
Thread.sleep(2000);
try {
//WindowsFileSystemHandler file = new WindowsFileSystemHandler();
file.enterfilepath(fileloc[i]);
System.out.println(fileloc[i]);
System.out.println(i);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String testfiletype = driver.findElement(By.xpath(".//*[@id='SFI_control']/div[2]/div[2]")).getText();
System.out.println(testfiletype);
ProfilePage.is_file_validation_successful(testfiletype);
}//end of for loop
顺便说一下,控制台输出如下:
d:\JW\testfiles\scribbles.txt
0
Invalid file type.
true
Test to see if system correctly rejects invalid file type was successful
d:\JW\testfiles\earth_large_file_size.jpg
1
Invalid file type.
true
Test to see if system correctly rejects invalid file type was successful
可能是我漏掉了一些愚蠢的东西。
发布于 2016-10-27 01:45:02
我想通了我的问题。基本上,我移动了以下语句:
driver.findElement(By.xpath(".//*[starts-with(@id, 'd2l_1_3_')]")).click();
在for循环中,这就解决了我的问题。愚蠢的错误。
有趣的是,我在使用这种方法测试的系统中发现了一个bug,因为之前的错误消息在每次上传文件后都没有被清除。
https://stackoverflow.com/questions/40274517
复制相似问题