0

I'm looking for a way to store an instantiated object and all of its properties to an external location and reuse it later.

What I tried so far:
I came across this [Serialization?] answer and tried it but the object I'm getting is null.

Algo / Rough code:

@Listeners(TestListener.class)
public class TestRunner {
    protected static Driver driver; //Driver implements WebDriver interface

    //Step 1
    public void method1(){
    driver = new Driver(1); //creates a customized chromedriver instance. Chrome driver / browser session is opened here.
    }

    //Step 2
    public void method2(){
        try {
            FileOutputStream fileOut = new FileOutputStream("card.ser");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(driver); 
            out.close();
            fileOut.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    //Step 3
    //Just manually stop the test/debugger for testing

    //Step 4
    public void method4(){
        try {
            FileInputStream fileIn = new FileInputStream("card.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            driver = (Driver) in.readObject(); //here driver returns as null. In this line, im hoping to recreate the previous object to somehow control the browser again
            in.close();
            fileIn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Some info:
This is actually related to a Selenium WebDriver problem in this SO question.

So what I'm trying to do is:

  • Step 1: I create a customized instance of a chrome driver
  • Step 2: Store the created object into an external source using the above code
  • Step 3: I intentionally stop the test to simulate failure
  • Step 4: I try to re-create the previous object by reading the stored source.

    I don't have prior background with serialization and I'm not sure if what I'm trying to do is possible. Any help or guidance is much appreciated.

  • Community
    • 1
    • 1
    iamkenos
    • 1,446
    • 8
    • 24
    • 49
    • 2
      You aren't going to have a lot of luck serializing the state of chrome; it's an external **native** process. – Elliott Frisch May 16 '17 at 03:00
    • Although I think virtualization software like VMware can freeze a process in execution and then instantiate multiple copies of the exact process state. I do understand it's popular in test frameworks for exactly that reason. Never tried it myself though. YMMV. – markspace May 16 '17 at 03:04

    2 Answers2

    1

    Assuming that Driver class also implements Serializable, one problem i see in your code is that driver is declared as protected static Driver driver, but method 1 which is suppose to create an instance of this class never gets executed, so you don't have that object when you serialise it. try calling method 1 before serialising it like this:

     public void method2(){
        try {
            FileOutputStream fileOut = new FileOutputStream("card.ser");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            method1();
            out.writeObject(driver); 
            out.close();
            fileOut.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    AbelSurace
    • 2,263
    • 1
    • 12
    • 16
    • Thank you for the response. method1 will get executed. Steps above are as-per algo. I'm calling those methods from another class. Initially, my Driver class does not implement serializable so I updated the Driver class to implement serializable. However, the Driver class uses objects from the java-selenium jar which does not implement Serializable, e.g. ChromeDriver, Actions..I do not have background about serialization - Is it required for all related objects to implement serializable before I can use it properly for ObjectOutputStream? – iamkenos May 16 '17 at 05:25
    • Yes, I'm not 100% sure but I believe all the objects and dependencies of the object need to implement serializable. – AbelSurace May 17 '17 at 03:22
    • Upon testing it out, I believe it works that way too. I have decided to go with MOXy JAXB – iamkenos May 17 '17 at 03:58
    0

    Although still incomplete, I managed to at least store the object using JAXB-MOXy. I have decided I'll be using this solution towards my problem as I'm having troubles using ObjectOutputStream mainly due to the objects used on the class I'm trying to export are 3rd party and does not implement Serializable.

    For those who will be facing a similar problem, you might find the following links helpful:

  • Make your JAXB cleaner with the MOXy implementation
  • Handling interfaces in JAXB with MOXy
  • Community
    • 1
    • 1
    iamkenos
    • 1,446
    • 8
    • 24
    • 49