0

I am really needing some help on this.

I have adopted the JNOTIFY approach to detecting any new files in a directory. When the file arrives the Listener informs that a new file is in the location.

  @BeforeTest(alwaysRun=true)
public void Polling() throws Exception {
    ListenToNotifications.checkFolderPickup();
}

I have attempted this where I addded a call to my Setup function in order to call my setup function after the file is detected.

            //snippet from Listener Class from checkFolderPickup();
            public void fileCreated(int wd, String rootPath, String name) {
       print("New File just created " + rootPath + " : " + name);
        Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );
        try {
            BaseTest.setup();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

My question is //Thread.sleep(1000000) i feel this is not a safe approach and I wanted to know if there is any other approach that I could possibly use instead of a Thread.Sleep, because this function will have to be executed once each time a new file is available and the old file will be deleted eventually and so on, I cannot make the Sleep to short , it will just ignore and continue with Base.Setup()

public static void checkFolderPickup() throws Exception {
...removed other code
    
    boolean watchSubtree = true;
    int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
    //Thread.sleep(1000000);
    Thread.sleep(20000);
    boolean res = JNotify.removeWatch(watchID);
    if (!res) {
        // invalid watch ID specified.
    }

}

I basically need my framework to keep polling that directory and each time it will execute the base setup process and follow a workflow, delete the file then poll again and so on.

Can anyone please advise?

Riidonesh
  • 63
  • 10
  • I think a CountDownLatch might help you. Not really sure how your code works so I cant really give you a complete answer. – Paul Samsotha Dec 02 '20 at 07:00
  • Thanks, I will look into creating a countDownLatch too – Riidonesh Dec 02 '20 at 08:37
  • Why do you want to remove and re-add the watch? – Omry Yadan Dec 03 '20 at 03:29
  • @OmryYadan This is the standard example that I found for JNotify – Riidonesh Dec 07 '20 at 10:03
  • Curious, having wrote the examples for jnotify it does not ring a bell (can you point me to the example?). Once you register a watch on a directory, it will keep delivering events on changes to files there. Here is the example I am thinking about: http://jnotify.sourceforge.net/sample.html – Omry Yadan Dec 08 '20 at 02:59

2 Answers2

0

You don't need any other modules , you can use custom expected condition:## using:

import java.io.File;

define the method inside any pageobject class:

private ExpectedCondition<Boolean> newfilepresent() {
    return new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            File f = new File("D:\\workplace"); 
            return f.listFiles().length>1;
        }
        
        @Override
        public String toString() {
          return String.format("wait for new file to be present within the time specified");
        }
    };
}

we created a custom expected condition method now use it as:

and in code wait like:

wait.until(pageobject.filepresent());

Output:

Failed:

enter image description here

Passed

enter image description here

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • Thank you, I will look into this right away – Riidonesh Dec 02 '20 at 08:36
  • 1
    This answer is not relevant to OP's problem. The question is not about how detect that a particular file exists (like in another question having the same answer) but rather about how to detect that any arbitrary file has been added to a folder and to log its name. Also the mentioned framework has to continuously poll the folder while the concept you suggest implies the end when condition has been met. – Alexey R. Dec 02 '20 at 11:27
  • @AlexeyR. as its arbitary file , thats why we need to use webdriver wait. Instead of creating a different polling function . – PDHide Dec 02 '20 at 11:52
  • @AlexeyR. the answer is relevant to the question , the use user wants to wait it till the file is present in the location . that is what webdriver wait does – PDHide Dec 02 '20 at 11:53
  • No. In your example you are using `program.txt`. The name you already know. But in OP's case you do not know what the file name would be. – Alexey R. Dec 02 '20 at 11:53
  • @ ok will change that to check for file count – PDHide Dec 02 '20 at 11:55
  • File count would not also be enough. The OPs case implies logging of the incoming file name and keep polling the folder. – Alexey R. Dec 02 '20 at 11:57
  • @AlexeyR. File.listFiles() lists all files in the floder , and the question deletes the existing file – PDHide Dec 02 '20 at 11:58
  • so count remains the same – PDHide Dec 02 '20 at 11:58
  • Thanks you for these responses, I will have to ensure that the old file is deleted each time because each file will be detected and cannot have more than one. – Riidonesh Dec 07 '20 at 10:06
  • @PDHide The above solution unfortunately wont work for me I have tried it and based on my scenario the PickUpLocation with be empty at times and as soon as another process drops a new file in that location it should invoke the next process, I may have to go with the JNotify if I can get it to function the way I need to – Riidonesh Dec 07 '20 at 10:30
  • Till the folder is empty what will your program do ? Wait for the folder not to be empty ? – PDHide Dec 07 '20 at 10:31
  • While the folder is empty, the framework / process will continue to Poll the folder Location, once a new File is dropped in that pickup location by another process (which is external to this framework) I want it do detect it and then continue with the next process, An external process is responsible for dropping a new file in the PickupLocation – Riidonesh Dec 07 '20 at 11:47
  • I mean during polling is your code doing something else , is it a single thread process ? . If so you can use this logic to wait till folder size is not zero copy the file and empty the folder – PDHide Dec 07 '20 at 11:52
  • WebDriver wait is a pollinbg function that polls every 500 millisecond – PDHide Dec 07 '20 at 11:53
  • @PDHide, thats correct its a single process just polling the directory – Riidonesh Dec 08 '20 at 07:36
  • @Riidonesh are the file names different (the old and new downloaded ) – PDHide Dec 08 '20 at 07:42
  • hpow will you know wchi is new which is old – PDHide Dec 08 '20 at 07:42
  • @PDHide, Yes, they differ I have assigned a timestamp to each of these filenames, but I will also be deleting them after each time they have been read in by the process – Riidonesh Dec 08 '20 at 08:31
  • What stoping you then from using the webdriver wait ? – PDHide Dec 08 '20 at 08:33
0

Once you register a watch on a directory with JNotify, it will continue to deliver events for files in that directory. You should not remove the watch if you wish to continue to receive events for that directory.

Omry Yadan
  • 31,280
  • 18
  • 64
  • 87