问题迭代通过字符串数组 - 传递文件路径Selenium

时间:2016-10-27 01:09:37

标签: java arrays selenium

我创建了一个方法(搜索此站点后),在Windows文件管理器中输入文件路径。这工作得很好但我想扩展我的测试用例以循环遍历文件位置数组并将测试文件传递给Web应用程序以测试它们是否得到妥善处理。

我遇到了第二遍(我有2个测试文件)的问题,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

可能是我想念的傻事。

1 个答案:

答案 0 :(得分:0)

我想出了我的问题。我基本上移动了声明:

driver.findElement(By.xpath(".//*[starts-with(@id, 'd2l_1_3_')]")).click();

在for循环中,这解决了我的问题。愚蠢的错误。

有趣的是,我发现使用此方法测试的系统存在错误,因为在每次上传文件后,以前的错误消息都没有被清除。