2014-03-30 2 views
0

Registroid를 사용하고 있다면 (인력이 필요하다면) 인벤토리를 쉽게 가져올 방법이 없다는 것을 알고있을 것입니다. 제 여자 친구가 작은 팝업 부티크를 시작했고 그녀는 몇 백 줄의 제품 데이터를 Registroid에 가져와야했습니다. Ruby에 익숙해지기 위해 좋은 변명을 찾고 있었기 때문에 나는 기회를 잡았습니다. 기술적으로 정통한 사람들 (또는 "코드 작성"을하는 사람을 알고있는 경우)에 대한 대답은 간단하고 간단합니다. 그것은 굉장히 견고하지도 않고 데이터를 기반으로 약간의 조정이 필요할 수도 있습니다. 그러나 내 여자 친구는 내가 천재라고 생각하는 어떤 방법으로도 의견을 읽으십시오! 스크립트는 현재 세금 체크 박스를 선택하지 않으며 짧은 설명 필드를 사용하지 않지만 필요에 따라 쉽게 추가 할 수 있습니다.어떻게 인벤토리를 등록 기관으로 가져올 수 있습니까? Ruby/Selenium/Test-Unit을 사용하여

답변

0
# 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