Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - create dynamic variable table that I can easily call back variables
    text
    copied!<p>I am a newbie programmer, very newbie..</p> <p>I am trying to write a program to test our website and am using Java and Selenium.</p> <p>The issue is I want to create a "table" or a "reference" that will allow me to store variables that can easily be called back and used in different calls.</p> <p>I tried to use a HashMap but found it was no good because when I rerun my testing code there is a new hashmap each time. I want something that can store the values and remember them the next time I run the code.</p> <p>I looked at creating a mysql table but I can't figure out how to recall the variables out of the table once they have been created.</p> <p>I hope this makes some sense. :0) Pls check out below if an example would be more useful +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</p> <p>Here is an example of the hashmap that I was using:</p> <pre><code>HashMap idTable = new HashMap(); idTable.put("GroupName", new String("Group " + Long.toHexString(Double.doubleToLongBits(Math.random())))); element = driver.findElement(By.id("name")); element.sendKeys((String)idTable.get("GroupName")); </code></pre> <p><br> <br>FYI: The reason this doesn't work for me is that I want to be able to wrap it in an "if" statement; to tell the computer that if the variable called "GroupName" already exists then don't do it again.. however every time I run the script I make a call to the function [HashMap idTable = new HashMap();] and I don't know how to NOT make that call because the HashMap isn't saved anywhere.. it is created new each time. <br> <br>Thanks, Orli</p> <p>not sure where to add this: but following the first suggestion here is what I did. <br> HashMap idTable; <br> <br> try{ <br> ObjectInputStream is = new ObjectInputStream( <br> new FileInputStream("C:\Documents and Settings\My Documents\Selenium local\hashmap.dat")); <br> idTable = (HashMap) is.readObject(); <br> }<br> <br> catch(Exception e){ <br> idTable = new HashMap(); <br> }</p> <p>AND then: <br> <br> try{ <br> ObjectOutputStream os = new ObjectOutputStream ( <br> new FileOutputStream("C:\Documents and Settings\My Documents\Selenium local\hashmap.dat")); <br> os.writeObject(idTable); <br> os.close(); <br> } <br> catch (Exception e){ <br> } <br> <br> It works. :0) Thanks for the help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload