0

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.

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
learner
  • 475
  • 3
  • 9
  • 23
  • Could you share the code for getImportFille() please? Is the file stored under the same class as openHtmlFile()? Is TestHelper a static class? I suspect it's got something to do with how the resource was saved and read. It gets trickier when trying to read the resource from a different class. – HaC Sep 10 '17 at 08:06
  • @HaC getImportFile() basically tries to form path of the html file where it is stored, basically it does something like this `System.getProperty("user.dir") + "fileName.html"` TestHelper is not a static class. and the file is stored in a seperate place in the git repository. – learner Sep 10 '17 at 12:08

4 Answers4

0

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.

yong
  • 13,357
  • 1
  • 16
  • 27
  • Thanks, but I already understand what you said. I know why it says "file not found" which I have also mentioned in my question. My question is how can I achieve it. – learner Sep 09 '17 at 01:56
0

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, e.g. scp, sftp, rmi, etc.

You can even create a micro service for files transferring or remote file system management.

Serhii Korol
  • 843
  • 7
  • 15
0

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://

yong
  • 13,357
  • 1
  • 16
  • 27
0

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.

HaC
  • 902
  • 12
  • 24
  • will this also work if the test is run in grid. For me, the build server( where jenkins first clones my git repo-> starts test) and the grid machine are different.The whole problem is arising because when on grid server, when test tries to open the html , it is not able to locate the path because that path shows the absolute path of the html file ( either absolute path on jenkins slave or local repo path if running test from local ) – learner Sep 11 '17 at 07:44