简体   繁体   English

使用远程Webdriver在远程计算机上打开html文件时显示“找不到文件”

[英]opening a html file on remote machine using remote webdriver says “file not found”

My tests are in git, and I am running them on the grid using Remote Webdriver. 我的测试在git中进行,我正在使用远程Webdriver在网格上运行它们。 I have to open a static HTML in grid. 我必须在网格中打开静态HTML。 For this, I have checked in the HTML file in the same git repo where my testis. 为此,我在睾丸所在的git repo中签入了HTML文件。 I have something like below in my test to open the file (which works on my local). 我在测试中有以下类似内容,可以打开文件(在我的本地计算机上可用)。

    public void openHtmlFile()  {
        String htmlFile=new TestHelper().getImportFile(TestConstants.OCI_HTML_FILE);
// Below gives exact path of the HTML file. Like when I am running it in my local, it gives exact path of the HTML file where it is kept in git repo in my local.
        log.info("Going to URL: " + htmlFile);
        Path sampleFile= Paths.get(htmlFile);
        driver.get(sampleFile.toUri().toString());
    }

When I run my test in my local, the HTML file opens fine, but on the grid it says, "file not found". 当我在本地运行测试时,HTML文件可以正常打开,但在网格上显示“找不到文件”。 Here is the screenshot of the page which gets opened in grid. 这是在网格中打开的页面的屏幕截图。 enter image description here 在此处输入图片说明

It navigates to URL which is the absolute path of that file in git workspace, so obviously, the node won't find the file as it's a different machine. 它导航到URL,它是git工作空间中该文件的绝对路径,因此很显然,该节点不会找到该文件,因为它是一台不同的机器。 How can I handle this situation? 我该如何处理这种情况? To add more details, I am running test thru Jenkins, so obviously I am cloning the git repo in Jenkins slave first, and then tests execution follows. 要添加更多详细信息,我正在通过Jenkins运行测试,因此很明显,我首先在Jenkins slave中克隆了git repo,然后进行了测试执行。 But Jenkins slave and grid node are a different machine, hence the file not found the issue. 但是Jenkins从属节点和网格节点是不同的机器,因此该文件未找到问题。

you need to understand the difference between run with grid and run local. 您需要了解使用网格运行和本地运行之间的区别。

1) run local will involve 2 machines: 1)在本地运行将涉及2台机器:
a. 一种。 script machine (your script placed on which) 脚本机器(您的脚本放置在哪个机器上)
b. b。 browser machine (browser opened on which, during running) 浏览器机器(在运行过程中打开浏览器)

in this case, both machine are same one, it's your local machine. 在这种情况下,两台计算机是同一台计算机,这是您的本地计算机。

2) run with grid will involve 3 machines: 2)用网格运行将涉及3台机器:
a. 一种。 script machine 脚本机
b. b。 browser machine 浏览器机器
c. C。 grid server 网格服务器

in this case, the 3 machines are 3 different machine in most case. 在这种情况下,三台机器在大多数情况下是三台不同的机器。
when the browser machine try to open the static html, it only can to find the static html file from itself, but the static html file not exist on browser machine, it on script machine. 当浏览器机器尝试打开静态html时,它只能从自身中找到静态html文件,但是静态html文件在浏览器机器上不存在,在脚本机上不存在。 so can't to open. 所以不能打开。

Little more about grid, selenium grid compose of one master and several nodes. 关于网格的更多内容,硒网格由一个主节点和多个节点组成。

  1. master also call grid server, it managed all registered node to find and assign idle node according to your desired capability. 主机还调用网格服务器,它管理所有注册的节点以根据您所需的能力查找并分配空闲节点。 (you never can get to know which node will be assigned before running, it decide by master. ) (您永远无法在运行之前知道将分配哪个节点,这由主节点决定。)

  2. node also call browser machine, any machine can register to the master 节点还调用浏览器机器,任何机器都可以注册到主节点

  3. in most case, we not to register the master itself as a node, even it allowed to do that. 在大多数情况下,即使主服务器允许这样做,也不会将其本身注册为节点。 because we hope master is only to manage node. 因为我们希望主人只是要管理节点。 Working as a browser machine at meanwhile will impact its performance on master role. 同时充当浏览器机器将影响其在主角色上的性能。

    Even you can register your local machine as a node, but you can't control master always to assign your local machine to you (unless there is only one node), so it also can't open static html file when script and browser machine are not same one. 即使您可以将本地计算机注册为节点,但也无法始终控制master来将本地计算机分配给您(除非只有一个节点),因此在脚本和浏览器计算机时它也无法打开静态html文件不是同一个人。

You should either run your code on Jenkins slave, or just transfer required file to target machine before actual interaction. 您应该在Jenkins从属服务器上运行代码,或者只是在实际交互之前将所需文件传输到目标计算机。

Former approach requires only Jenkins tweaks (agent setup + forcing job to be executed on newly raised slave). 以前的方法仅需要进行詹金斯调整(代理设置+强制在新提出的从属上执行作业)。

Latter approach is more about some specialized tools or interfaces usage for sending files between machines, eg scp , sftp , rmi , etc. 后一种方法更多地是关于一些专用工具或接口用法,用于在机器之间发送文件,例如scpsftprmi等。

You can even create a micro service for files transferring or remote file system management. 您甚至可以为文件传输或远程文件系统管理创建微服务。

I noticed my company jenkins job has 'workspace' which will list all folder/files from git and each file is a link which url prefixed with jenkins server address, if you click on it, it can be open in browser with http protocol. 我注意到我公司的jenkins作业具有“工作区”,该工作区将列出git中的所有文件夹/文件,每个文件都是一个以jenkins服务器地址为前缀的url链接,如果单击它,则可以在使用http协议的浏览器中打开它。

I think you can modify your test script to accept the page url from command line, therefore you can pass its value in jenkins job configuration. 我认为您可以修改测试脚本以从命令行接受页面url,因此可以在jenkins作业配置中传递其值。

My company Jenkins Job's workspace screenshot, click here to see what's it 我的公司Jenkins Job的工作区屏幕截图,单击此处查看它的内容

Below is the url of Readme.md in above picture: 以下是上图中的Readme.md的网址:
https://[ jenkins server address ]/cm2/job/PI/job/PIY-PIT/job/DEV/job/PL000139/job/PR104403/job/Taxable-Equivalent%20Yields%20(AP002363)/job/TEY%20Browser%20Test/ws/protractor-cucumber-tey/Readme.md https:// [ jenkins服务器地址] / cm2 / job / PI / job / PIY-PIT / job / DEV / job / PL000139 / job / PR104403 / job / Taxable-Equivalent%20Yields%20(AP002363)/ job / TEY %20Browser%20Test / ws / protractor-cucumber-tey / Readme.md

For your case, your static html file will have a fixed url, you can hardcode the url in command line, or use below enviorment variable combine: 对于您的情况,您的静态html文件将具有固定的url,您可以在命令行中对url进行硬编码,或在下面的envirorment变量结合使用:

${JOB_URL}/ws/[relative path of static html file begin from project folder] $ {JOB_URL} / ws / [静态html文件的相对路径从项目文件夹开始]
example: ${JOB_URL}/ws/protractor-cucumber-tey/Readme.md 例如:$ {JOB_URL} /ws/protractor-cucumber-tey/Readme.md

JOB_URL is jenkins job build-in enviorment variable, jenkins will calculate its value for you. JOB_URL是jenkins作业内置环境变量,jenkins将为您计算其值。

when run on local you can pass the url prefix with file:// 当在本地运行时,您可以将URL前缀与file://一起传递

Instead of user.dir system property, I would store the file under the resources folder and use class.getResource() to get the file path. 代替user.dir系统属性,我将文件存储在resources文件夹下,并使用class.getResource()获取文件路径。 This is the simplest way if the resource file is stored in the same class. 如果资源文件存储在同一类中,这是最简单的方法。

If you have to store it somewhere else, then getResource() doesn't know how to pass it back to the right class. 如果必须将其存储在其他位置,则getResource()不知道如何将其传递回正确的类。 You'll have to write the file to the system's temp directory and then read it back from there. 您必须将文件写入系统的temp目录,然后从那里读取它。 Something like this (assuming the class that contains the file is named YourClass): 这样的事情(假设包含文件的类名为YourClass):

        Path tmpFile = Files.createTempFile(FilenameUtils.getBaseName(filename), "html");
        Files.copy(YourClass.class.getResourceAsStream(filename), tmpFile, StandardCopyOption.REPLACE_EXISTING);
        return tmpFile.toString();

This would work in both local and the build servers. 这将在本地服务器和构建服务器中都起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM