简体   繁体   中英

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. I have to open a static HTML in grid. For this, I have checked in the HTML file in the same git repo where my testis. 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". 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. 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. But Jenkins slave and grid node are a different machine, hence the file not found the issue.

you need to understand the difference between run with grid and run local.

1) run local will involve 2 machines:
a. script machine (your script placed on which)
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:
a. script machine
b. browser machine
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. 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.

You should either run your code on Jenkins slave, or just transfer required file to target machine before actual interaction.

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.

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.

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.

My company Jenkins Job's workspace screenshot, click here to see what's it

Below is the url of Readme.md in above picture:
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

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:

${JOB_URL}/ws/[relative path of static html file begin from project folder]
example: ${JOB_URL}/ws/protractor-cucumber-tey/Readme.md

JOB_URL is jenkins job build-in enviorment variable, jenkins will calculate its value for you.

when run on local you can pass the url prefix with 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. 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. You'll have to write the file to the system's temp directory and then read it back from there. Something like this (assuming the class that contains the file is named 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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