How to load/write data into a newfile using seleni

2019-07-04 05:20发布

问题:

I'm using selenium IDE to record & replay some testcase. During this, i stored some values in variables.Now i've to load/write these variable's value into new file.How can i do it ? is it possible ?

回答1:

It's not possible using the Selenium IDE, but you can store using Selenium RC or Webdriver (Junit or Nunit).



回答2:

It is definitely possible. You just need to create some javascript function using mozilla (firefox) addons API https://developer.mozilla.org/en-US/Add-ons, save this function in selenium core extension file http://www.seleniumhq.org/docs/08_user_extensions.jsp and add this extension into Selenium IDE.

Sample function to write text file:

    // ==================== File Writer ====================
Selenium.prototype.doWriteData = function(textToSave, location)
{   
    var data = textToSave;
    var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(location);
    var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
    foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);
    var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].createInstance(Components.interfaces.nsIConverterOutputStream);
    converter.init(foStream, "UTF-8", 0, 0);
    converter.writeString(data);
    converter.close();
}

Read is simple to do as well, just add another function, something like "doReadData" more info here https://developer.mozilla.org/en-US/Add-ons/Code_snippets/File_I_O

To add this script to Selenium-IDE, follow this instructions:

  1. Create your user extension and save it as user-extensions.js. While this name isn’t technically necessary, it’s good practice to keep things consistent.
  2. Open Firefox and open Selenium-IDE.
  3. Click on Tools, Options
  4. In Selenium Core Extensions click on Browse and find the user-extensions. js file. Click on OK.
  5. Your user-extension will not yet be loaded, you must close and restart Selenium-IDE.
  6. In your empty test, create a new command, your user-extension should now be an options in the Commands dropdown.


回答3:

I do not know if selenium IDE is sufficient to achieve what you are trying to do but if you can export the recorded test case to any language like java and then you can easily modify the exported code to write the variables you have stored to any file using the inbuilt capabilities of languages such as java.



回答4:

Try to use storeEval command,it evaluates java script snippets and refer the tips posted in http://www.tek-tips.com/viewthread.cfm?qid=1171273.

Let me know the result. -Thanks



回答5:

It is not possible using selenium IDE, you need to use selenium webdriver where you can code language to do read/write operation to a file.



回答6:

here you can find the answer how to read variables from CSV file (you should arrach .js file in the selenium IDE options as extension and at that page is also example how to use it)

http://openselenium.com/?p=19



回答7:

You can use 'file logging' for this, try below plugin

https://addons.mozilla.org/en-US/firefox/addon/file-logging-selenium-ide/

Steps:

  1. Install Addon
  2. Set Log Level to 'Info'
  3. Navigate to 'Options -> FileLogging' in IDE and provide path (D:\Users\jmatthew\Desktop\Selenium\Log1.csv)
  4. Use command storeText | id=abcd.aa | value
  5. Use command echo | ${value}
  6. Read the CSV file using VLOOKUP for matching results contain 'echo'