Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

internet explorer - use browser as GUI in Ruby

In vbscript it is common to use the browser (IE) as a GUI. See the example below, it asks for a name and returns it to the script. In Ruby you have a few GUI's like Tcl and Shoes but i wonder how to do this in the browser. What is the simplest Ruby solution to do this ? So no exta gems or packages, no server that is allready running.. If a gem is needed, preferably one that works in Windows without problems.

Here the vbscript sample

Set web = CreateObject("InternetExplorer.Application")
If web Is Nothing Then
  msgbox("Error while loading Internet Explorer")
  Wscript.Quit
Else
  with web
    .Width = 300
    .Height = 175
    .Offline = True
    .AddressBar = False
    .MenuBar = False
    .StatusBar = False
    .Silent = True
    .ToolBar = False
    .Navigate "about:blank"
    .Visible = True
  end with
End If

'Wait for the browser to navigate to nowhere
Do While web.Busy
  Wscript.Sleep 100
Loop

'Wait for a good reference to the browser document
Set doc = Nothing
Do Until Not doc Is Nothing
  Wscript.Sleep 100
  Set doc = web.Document
Loop

'Write the HTML form
doc.Write "Give me a name<br><form><input type=text name=name ><input type=button name=submit id=submit value='OK' onclick='javascript:submit.value=""Done""'></form>"
Set oDoc = web.Document
Do Until oDoc.Forms(0).elements("submit").Value <> "OK"
  Wscript.Sleep 100
  If web Is Nothing or Err.Number <> 0 Then
    msgbox "Window closed"
    Wscript.Quit
  End If
Loop
name = oDoc.Forms(0).elements("name").value
oDoc.close
set oDoc = nothing
web.quit
set web = nothing
Wscript.echo "Hello " & name
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use the Watir gem. The gem was originally intended to drive the IE browser, but would fit your need.

To see:

1) Install the Watir gem

2) Create a test.htm file with the following:

Give me a name<br>
<form name="myForm" title="myForm">
    <input type="text" id="name" >
    <input id="submit" type="button" value="OK" onclick='document.myForm.submit.value="Done"'>
</form>

3) Run the following watir script, which will open the browser to your form. After you input the name and click [OK], the name is outputted. Note that you may need to change the location of the file in the script depending on where you saved your test.htm:

require 'watir'

b = Watir::IE.new
begin
    b.goto('file:///C:/Documents%20and%20Settings/Setup/Desktop/test.htm')
    begin
        sleep(5)
    end until b.button(:id, 'submit').value != "OK"
    name = b.text_field.value
ensure
    b.close
end
puts name

I think this shows the general feasibility of doing what you want. Validation and dynamic creation of the forms would also be possible.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...