If you're using Registroid (if you have to) then you probably know that there's no easy way to import your inventory. My girlfriend is starting a small pop-up boutique and she needed to import a few hundred rows of product data into Registroid. I've been looking for a good excuse to familiarize myself with Ruby, so I seized the opportunity. For those who are technically savvy enough (or if you know someone who "codes"), I provide a short and simple solution in the answer below. It's not terribly robust and may require some adjustment based on your data, but either way my girlfriend thinks I'm a genius :) Read the comments! Script currently does not select any of the tax check boxes and it does not use the short description field, but you can easily add those as needed!

有帮助吗?

解决方案

# https://gist.github.com/anonymous/9884343
require "json"
require "selenium-webdriver"
gem "test-unit"
require "test/unit"
require "csv"

class ImportInventoryRegistroid < Test::Unit::TestCase

  def setup
    @driver = Selenium::WebDriver.for :firefox
    @base_url = "https://www.registroid.com"
    @accept_next_alert = true
    @driver.manage.timeouts.implicit_wait = 30
    @verification_errors = []
  end

  def teardown
    @driver.quit
    assert_equal [], @verification_errors
  end



  def test_import_inventory_registroid

      # TODO: provide your username, password and path to the CSV file
      # assuming you place your csv file in the same location as your ruby script you simply need to
      # replace {YOUR FILE NAME} with the name of your file
      username = ""
      password = ""
      filePath = File.path("{YOUR FILE NAME}.csv")

      @driver.get(@base_url + "/Account/Login.aspx")
      @driver.find_element(:id, "MainContent_LoginUser_UserName").clear
      @driver.find_element(:id, "MainContent_LoginUser_UserName").send_keys username
      @driver.find_element(:id, "MainContent_LoginUser_Password").clear
      @driver.find_element(:id, "MainContent_LoginUser_Password").send_keys password
      @driver.find_element(:id, "MainContent_LoginUser_LoginButton").click

      CSV.foreach(filePath) do |col|

          # sample CSV (this script was not written to be very robust so you will need to either edit it or remove the headers)
          # 11111,Some Product (color = some color | size = some size),11111,51,106,1,3,


          # map of columns to the values they contain (see below code) - you can edit this based on your CSV file

          # IMPORTANT:
          # ensure your numbers don't have dollar signs ($) or commas as thousand separators (e.g. valid numbers would be 1000 or 1000.00, but 1,000 is invalid) : numbers and decimals only or you need to make the script more robust
          # csv must not have any nil values unless you are not using those columns
          # ensure your id does not conflict with existing records or this script will break
          # you may want to try running a test with a single entry before inserting multiple entries

          id = col[0] #id
          description = col[1] #description
          scanId = col[2] #scan id
          wholesalePrice = col[3] #wholesale price
          retailPrice = col[4] #retail price
          quantity = col[5].to_i #quanity
          department = col[6].to_i #department

          @driver.get(@base_url + "/Account/items.aspx")
          @driver.find_element(:id, "MainContent_MainContent_btnNew").click
          @driver.find_element(:id, "MainContent_MainContent_DetailsView1_txtItemID").clear
          @driver.find_element(:id, "MainContent_MainContent_DetailsView1_txtItemID").send_keys id
          @driver.find_element(:id, "MainContent_MainContent_DetailsView1_TextBox4").clear
          @driver.find_element(:id, "MainContent_MainContent_DetailsView1_TextBox4").send_keys description
          @driver.find_element(:name, "ctl00$ctl00$MainContent$MainContent$DetailsView1$ctl01").clear
          @driver.find_element(:name, "ctl00$ctl00$MainContent$MainContent$DetailsView1$ctl01").send_keys id

          if department == 1
            Selenium::WebDriver::Support::Select.new(@driver.find_element(:id, "MainContent_MainContent_DetailsView1_ddlDept")).select_by(:text, "Men's")
            @driver.find_element(:css, "#MainContent_MainContent_DetailsView1_ddlDept > option[value=\"1\"]").click
          elsif department == 2
            Selenium::WebDriver::Support::Select.new(@driver.find_element(:id, "MainContent_MainContent_DetailsView1_ddlDept")).select_by(:text, "Women's")
            @driver.find_element(:css, "#MainContent_MainContent_DetailsView1_ddlDept > option[value=\"2\"]").click
          else
            Selenium::WebDriver::Support::Select.new(@driver.find_element(:id, "MainContent_MainContent_DetailsView1_ddlDept")).select_by(:text, "Accessories")
            @driver.find_element(:css, "#MainContent_MainContent_DetailsView1_ddlDept > option[value=\"3\"]").click
          end

          # sleep statements below are necessary
          sleep(1)
          @driver.find_element(:id, "txtCost").click
          @driver.find_element(:id, "txtCost").clear
          @driver.find_element(:id, "txtCost").send_keys wholesalePrice

          sleep(1)
          @driver.find_element(:id, "txtPrice").click
          @driver.find_element(:id, "txtPrice").clear
          @driver.find_element(:id, "txtPrice").send_keys retailPrice

          sleep(1)
          @driver.find_element(:id, "MainContent_MainContent_DetailsView1_TextBox10").clear
          @driver.find_element(:id, "MainContent_MainContent_DetailsView1_TextBox10").send_keys quantity
          @driver.find_element(:name, "ctl00$ctl00$MainContent$MainContent$DetailsView1$ctl02").click
      end
  end

  def element_present?(how, what)
    @driver.find_element(how, what)
    true
  rescue Selenium::WebDriver::Error::NoSuchElementError
    false
  end

  def alert_present?()
    @driver.switch_to.alert
    true
  rescue Selenium::WebDriver::Error::NoAlertPresentError
    false
  end

  def verify(&blk)
    yield
  rescue Test::Unit::AssertionFailedError => ex
    @verification_errors << ex
  end

  def close_alert_and_get_its_text(how, what)
    alert = @driver.switch_to().alert()
    alert_text = alert.text
    if (@accept_next_alert) then
      alert.accept()
    else
      alert.dismiss()
    end
    alert_text
  ensure
    @accept_next_alert = true
  end
end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top