我的本地机器上有一个html文件。当通过eclipse本地运行我的selenium-java代码时,我可以通过下面提到的代码访问html文件 -
File file = new File(url);
driver.get("file:///" + file.getAbsolutePath());
如果我通过selenium网格运行代码,注册节点不会选择要在chrome中打开的html文件路径,因为绝对路径指向本地计算机。
有没有可用的解决方案通过selenium grid-node打开本地可用的html文件?
您应该使用LocalFileDetector。
import org.openqa.selenium.remote.LocalFileDetector
import org.openqa.selenium.remote.RemoteWebDriver
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.firefox());
driver.setFileDetector(new LocalFileDetector())
您的上传现在应该有效。
基本上有两种方法可以完成这项工作。我将列出两种方法。请随意选择适合您的方式。
使用自定义节点servlet
您需要按照以下步骤操作:
它可能看起来像下面[这个代码是从一个postdev post here借来的,只是为了完整性而被包含在这里]你可能需要在使用前调整代码,而不一定按原样使用它。当前代码返回一个html响应,但您可能需要更改它以便它返回一个JSON响应,其中包含上载文件的实际路径。这条路径就是你在driver.get()
电话中使用的路径。
package com.journaldev.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadDownloadFileServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private ServletFileUpload uploader = null;
@Override
public void init() throws ServletException{
DiskFileItemFactory fileFactory = new DiskFileItemFactory();
File filesDir = (File) getServletContext().getAttribute("FILES_DIR_FILE");
fileFactory.setRepository(filesDir);
this.uploader = new ServletFileUpload(fileFactory);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fileName = request.getParameter("fileName");
if(fileName == null || fileName.equals("")){
throw new ServletException("File Name can't be null or empty");
}
File file = new File(request.getServletContext().getAttribute("FILES_DIR")+File.separator+fileName);
if(!file.exists()){
throw new ServletException("File doesn't exists on server.");
}
System.out.println("File location on server::"+file.getAbsolutePath());
ServletContext ctx = getServletContext();
InputStream fis = new FileInputStream(file);
String mimeType = ctx.getMimeType(file.getAbsolutePath());
response.setContentType(mimeType != null? mimeType:"application/octet-stream");
response.setContentLength((int) file.length());
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
ServletOutputStream os = response.getOutputStream();
byte[] bufferData = new byte[1024];
int read=0;
while((read = fis.read(bufferData))!= -1){
os.write(bufferData, 0, read);
}
os.flush();
os.close();
fis.close();
System.out.println("File downloaded at client successfully");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(!ServletFileUpload.isMultipartContent(request)){
throw new ServletException("Content type is not multipart/form-data");
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.write("<html><head></head><body>");
try {
List<FileItem> fileItemsList = uploader.parseRequest(request);
Iterator<FileItem> fileItemsIterator = fileItemsList.iterator();
while(fileItemsIterator.hasNext()){
FileItem fileItem = fileItemsIterator.next();
System.out.println("FieldName="+fileItem.getFieldName());
System.out.println("FileName="+fileItem.getName());
System.out.println("ContentType="+fileItem.getContentType());
System.out.println("Size in bytes="+fileItem.getSize());
File file = new File(request.getServletContext().getAttribute("FILES_DIR")+File.separator+fileItem.getName());
System.out.println("Absolute Path at server="+file.getAbsolutePath());
fileItem.write(file);
out.write("File "+fileItem.getName()+ " uploaded successfully.");
out.write("<br>");
out.write("<a href=\"UploadDownloadFileServlet?fileName="+fileItem.getName()+"\">Download "+fileItem.getName()+"</a>");
}
} catch (FileUploadException e) {
out.write("Exception in uploading file.");
} catch (Exception e) {
out.write("Exception in uploading file.");
}
out.write("</body></html>");
}
}
-servlets
参数,以便新创建的节点servlet被注入节点并可通过http://<Node_IP_Address>:<Node_Port>/extra/UploadDownloadFileServlet
获取(因为我们的示例servlet的名称是UploadDownloadFileServlet
)RemoteWebDriver
实例。http://<Node_IP_Address>:<Node_Port>/extra/UploadDownloadFileServlet
触发先前创建的servlet的HTTP POST,并返回到响应中上载位置的路径。driver.get()
调用中步骤(6)中返回的路径(不要忘记包含file:///
协议)那应该做。
使用Javascript
在这种方法中,你基本上开始加载一个空白页面(例如,driver.get("about:blank");
然后利用Javascript开始通过document.createElement()
调用动态创建你的网页(请参阅this文章了解更多信息)并创建整个页面。现在可以开始与页面进行交互。
方法(1)仅在您使用允许添加servlet等并访问其IP和端口的网格环境时才有用。
方法(2)将适用于所有用例,包括第三方远程执行环境提供程序,如SauceLabs或BrowserStack(如果您也使用它们)