Passing Values Through Excel sheet in Ruby-watir - ruby

require 'watir-webdriver'
require 'win32ole'
require 'roo'
b= Watir::Browser.new(:firefox)
b.goto('https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=http://mail.google.com/mail/&scc=1&ltmpl=default&ltmplcache=2')
xl = WIN32OLE.new('excel.application')
wrkbook= xl.Workbooks.Open("C:\\Excel\\mondial1.xlsx")
wrksheet= wrkbook.Worksheets(1)
wrksheet.Select
username1= wrksheet.Range("a1").Value
password1= wrksheet.Range("b1").Value
b.text_field(:id, "Email").set("username1")
b.text_field(:id, "Passwd").set("password1")
b.button(:id, "signIn").click
xl.Quit
I just want to open a Excel sheet and get values from there and i need to provide that as a input to textfield in gmail.
instead of getting values from Excel sheet *It sets as "username1" directly* I need to pass values through Excel Please provide your suggestion, thanks in advance

Issue
In the line
b.text_field(:id, "Email").set("username1")
You are passing a string "username1" to the set method. This is why that is the value that is typed into the text field.
Solution
What you actually want to do is pass the value of the variable username1. This is done by making it the parameter of the set method.
b.text_field(:id, "Email").set(username1)
Notice that there are no quotations around the username1. Similar would be required for the password1 variable.

Related

Using one variable for multiple items data in descriptive programming

I know that with Descriptive programming you can do something like this:
Browser("StackOverflow").Page("StackOverflow").Link("text:=Go To Next Page ", "html tag:=A").Click
But is it possible to create some kind of string so I can assign more than one data value and pass it as single variable? I've tried many combinations using escape characters and I always get error.
For example in the case above, let's say I have more properties in the Page object, so I'd normally have to do something like this:
Browser("StackOverflow").Page("name:=StackOverflow", "html id:=PageID")...etc...
But I'd like to pass "name:=StackOverflow", "html id:=PageID" as a single variable, so when writing many objects I'd only have to write:
Browser(BrowserString).Page(PageString).WebEdit("name:=asdfgh")
And the first part would remain static, so if the parents' data needs to be modified I'd only have to modify two variables and not all the objects created in all libraries.
Is it possible?
If I was not clear enough please let me know.
Thank you in advance!
I think what you're looking for is UFT's Description object
This allows you finer grained control on the description since in descriptive programming all values are regular expressions but with Description you can turn the regular expression functionality off for a specific property.
Set desc = Description.Create()
desc("html tag").Value = "A"
desc("innertext").Value = "More information..."
desc("innertext").RegularExpression = False
Browser("Example Domain").Navigate "www.example.com"
Browser("Example Domain").Page("Example Domain").WebElement(desc).Click
If you want to represent this with plain string then it's a bit more of a problem, you can write a helper function but I'm not sure I would recommend it.
Function Desc(descString)
Set ret = Description.Create()
values = Split(descString, "::")
For Each value In values
keyVal = Split(value, ":=")
ret(keyVal(0)).Value = keyVal(1)
Next
Set Desc = ret
End Function
' Usage
Browser("StackOverflow").Page("StackOverflow").WebElement(Desc("html tag:=H2::innertext:=some text")).Click
Further reading about descriptive programming.
As an alternative to Motti's excellent answer, you could also Set a variable to match your initial descriptive object and then extend it as required:
Set myPage = Browser("StackOverflow").Page("name:=StackOverflow", "html id:=PageID")
after which you can then use
myPage.WebEdit("name:=asdfgh")
throughout the rest of the code, so long as the myPage object stays in scope...

Capture selected value from list using webdriver and Ruby

I'm using selenium webdriver with ruby. I've written a script that will fill in a form. One field is a dropdown list. What I would like to do is capture the value I selected in the list.
For example: If I had a list of cars and I selected Honda I would like to capture the value in the field (Honda) and place it in a variable for me to use later.
I hope I'm making sense.
You can use below code select list items:
cars_select = driver.find_element(:id=> "cars_list")
//use id of your dropdownlist
options = cars_select.find_elements(:tag_name=>"option")
options.each do |el|
if (el.value == "Honda")
el.select()
var selected_car = el.value;
break
end
end
If you are selecting from dropdown with:
browser.select_list(id: 'some_id').option(text: 'some_value').select
Then you can store that value in a variable, like below:
var1=browser.select_list(id: 'some_id').option(text: 'some_value').value
Hope this will work. If not, then provide your dropdown part html, and explain more. I am using watir-webdriver, try if this work for selenium-webdriver.

Run time error 1004, Application defined or object defined error in excel vbs

I have written vbscript code to add chart in page 1 of excel for which the source is from other sheet of same excel which name is "CL.1.1" but i am getting the above error can any one help what was wrong in my below code.
Sub DispvsTime(Shname)
Sheets("Sheet1").Select
noofsheets = ActiveSheet.ChartObjects.Count
If noofsheets > 0 Then
ActiveSheet.ChartObjects.Select
ActiveSheet.ChartObjects.Delete
End If
Sheets("Sheet1").Pictures.Visible = False
ActiveSheet.Shapes.AddChart(1000, 420, 50, 500).Select
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
ActiveChart.SetSourceData Source:=Sheets(Shname).Range("G2:H2001")
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Text = "Displacement VS Time"
End Sub
here shname is name of the sheet where data is picked.
Can any one help me to find out the bug inside the code to get this error ?
If the sheet name is Shname then place it around quotation marks, if it is a variable name, which holds the sheet name, then ignore this post :P
Sheets("Shname")
If your code is really VBScript as you say, and not VBA as it looks like, then there are several issues:
Unlike VBA you cannot use objects like ActiveSheet or ActiveChart directly in VBScript. You need a reference to these objects, usually the application object:
Set xl = CreateObject("Excel.Application")
xl.ActiveSheet.Name = "foo"
VBScript doesn't know about Excel/Office constants like xlXYScatterSmoothNoMarkers or msoElementChartTitleAboveChart, so you have to either use literal values or define those constants yourself:
Const xlXYScatterSmoothNoMarkers = 73
You can't use named arguments like Source:=... in VBScript:
xl.ActiveChart.SetSourceData xl.Sheets(Shname).Range("G2:H2001")
See here for a more detailed explanation of the differences between VBA and VBScript.

Using Loop Conditions in Excel-Ruby Watir Scripts

I'm New to Ruby-Watir ..And am Checking Login Functionality for one Application that Multiple values like (Invalid user& Password ,Valid Username & Password)coming from Excel Sheet.
reqire'watir-webdriver'
require 'win32ole'
require 'roo'
b = Watir::Browser.new
b.goto 'http://tech/mellingcarsweb/Admin/Login.aspx'
xl = WIN32OLE.new('excel.application')
wrkbook= xl.Workbooks.Open("C:\\Excel\\cars.xlsx")
wrksheet= wrkbook.Worksheets(1)
wrksheet.select
$username= wrksheet.Range("A1").Value
$password= wrksheet.Range("B1").Value
b.text_field(:id, "MainContent_txtUsername").set($username)
b.text_field(:id, "MainContent_txtPassword").set($password)
b.button(:id, "MainContent_btnLogin").click
b.alert.ok
$username1= wrksheet.Range("A2").Value
$password1= wrksheet.Range("B2").Value
b.text_field(:id, "MainContent_txtUsername").set($username1)
b.text_field(:id, "MainContent_txtPassword").set($password1)
b.button(:id, "MainContent_btnLogin").click
puts "Authorised Entry"
This code is working fine.What my need is.. using loop statements i need to execute for multiple times.
I don't Know how to use looping conditions in Excel.And i have seen lot of Examples But none of them are clear.Sorry am not able to understand.Will anyone Explain me in proper way of using looping Conditions in Excel ruby.
Thanks
Given that you are testing valid and invalid logins, I am not sure that it makes the most sense to write a loop. The problem with a loop is that, each iteration needs to know what the expected result is. You would likely need to include that information in your spreadsheet. You might be better off creating specific individual tests for each valid and invalid scenario (ie one assertion per test).
To answer your question, you can convert the excel range to a 2D array and then iterate through each row of data. To get the 2D array:
require 'win32ole'
#Open the spreadsheet
xl = WIN32OLE.new('excel.application')
wrkbook= xl.Workbooks.Open('C:\Users\Someone\Desktop\Logins.xlsx')
wrksheet= wrkbook.Worksheets(1)
#Convert the range of cells to a 2D array
login_data = wrksheet.range("A1:B2").value
#Output of the 2D array
p login_data
#=> [["jsmith", "password1"], ["jdoe", "password2"]]
Each element of the 2D array is a row of the excel data. You can iterate through it using the each method.
#Start your browser
b = Watir::Browser.new
#Loop through the login data
login_data.each do |data|
#Determine the user id and password for the row of data
user = data[0]
password = data[1]
#Go to the login page
b.goto 'http://tech/mellingcarsweb/Admin/Login.aspx'
#Input the fields and submit
b.text_field(:id, "MainContent_txtUsername").set(user)
b.text_field(:id, "MainContent_txtPassword").set(password)
b.button(:id, "MainContent_btnLogin").click
#Do something to validate the result
end
Google for ruby loops. This looks like a good introduction: http://www.tutorialspoint.com/ruby/ruby_loops.htm

Unable to save Excel file after modification using Ruby & win32ole

Using http://ruby-doc.org/stdlib/libdoc/win32ole/rdoc/classes/WIN32OLE.html as a guide I have written the following:
require 'win32ole'
excel = WIN32OLE.new('Excel.Application')
excel.visible = false #Setting this is 'true' doesn't reveal anything
workbook = excel.workbooks.open('C:\myspreadsheet.xlsx')
worksheet = workbook.worksheets('sheet1')
worksheet.Activate
data = worksheet.UsedRange.Value
p data.size #This works! - My spreadsheet has 3987 rows.
p data[3932] #This works, too! - I can "see" row 3932.
worksheet.Rows(3932).Insert #Insert a row above row 3932
data = worksheet.UsedRange.Value
p data.size #Returns 3988! This would seem to indicate that I've successfully added a row since it was just 3987.
workbook.saved = true #Save the workbook and quit.
excel.ActiveWorkbook.Close(0)
excel.Quit()
When I open the Excel spreadsheet after all of this it is unchanged. Any ideas?
Setting the Saved property of a Workbook object to True does not cause the workbook to be saved. That property is used as a flag to show whether a workbook has unsaved changes. Setting it to True is a simple way of preventing the "Do you wish to save..." dialog appearing when Excel is closed.
To actually save the workbook, you need the Save method of the Workbook object. This method doesn't return anything so I would assume that workbook.Save would do the trick (I have no experience of Ruby, unfortunately, so can't be 100% sure on that)

Resources