在本地写selenium自动化测试的时候,选择文件时只需要用File定位文件位置,之后直接调用就好
比如:
File file1 = new File("C:\\Desktop\\test.txt");
uploadFileWebElement.sendkeys(prop.getProperty(file));
但是如果我们需要用Azure Pipline或者其他云端服务平台来运行自动化的时候,像上面直接使用文件的储存地址就不能这么写了。这里我们可以使用AbsolutePath来解决这个问题
比如:
String absolutePath = new File("TestFiles\\").getAbsolutePath();
System.out.println(absolutePath);
uploadFileWebElement.sendKeys(absolutePath + prop.getProperty("test.txt"));
这样我们就可以直接调用当前maven project文件夹里TestFiles文档中的test.txt。也不会纠结于如何使用".\\" 或者"..\\"来表示当前文件夹中的问题。
(如果有人用这个办法解决了你的问题,请在评论区写一下你遇到的error message是什么,谢谢。我忘了当初具体的error message是什么了。。。)