Rspec + Capybara: How can I use expect to validate all items in a field? - ruby

Brothers help me please. I need to validate the values ​​of this combo, I tried it with a command I found here but I still haven't had success. The error I'm getting is: Reason:
Ambiguous match, found 7 elements matching visible option nil
----------------------------------------------
I noticed that the first value is white I don't know if this is the error but I tried to get it for a specific value and I couldn't do it either!!
My Code:
class Screen_Grupo_Intervencao < SitePrism::Page
include RSpec::Mocks::ExampleMethods::ExpectHost
include RSpec::Matchers
element :estrutura, :xpath, '//*[#name="_lyXWFMTGINID_ALAY"]'
all(screen_grupo_intervencao.estrutura select)1.should have_text('CADASTRO TÉCNICO D&C - DISTRIBUIÇÃO E COLETA ENGENHARIA GESTÃO DE PERDAS GSC COMERCIAL GSO OPERACIONAL')
<select style="padding-right:0px;width:100%;" class="f fA" onrealchange="return true;" onchange="setTooltip(this,'');realChangeListener(event);" id="686" name="_lyXWFMTGINID_ALAY" tabindex="151" evidence="">
<option title="" selected="" value="" class="f fA" style="border:0px;border-radius:0px;font-size:1em;box-shadow:none;"></option>
<option title="" value="61" class="f fA" style="border:0px;border-radius:0px;font-size:1em;box-shadow:none;">CADASTRO TÉCNICO</option>
<option title="" value="41" class="f fA" style="border:0px;border-radius:0px;font-size:1em;box-shadow:none;">D&C - DISTRIBUIÇÃO E COLETA</option>
<option title="" value="62" class="f fA" style="border:0px;border-radius:0px;font-size:1em;box-shadow:none;">ENGENHARIA</option>
<option title="" value="63" class="f fA" style="border:0px;border-radius:0px;font-size:1em;box-shadow:none;">GESTÃO DE PERDAS</option>
<option title="" value="1" class="f fA" style="border:0px;border-radius:0px;font-size:1em;box-shadow:none;">GSC COMERCIAL</option>
<option title="" value="21" class="f fA" style="border:0px;border-radius:0px;font-size:1em;box-shadow:none;">GSO OPERACIONAL</option>
</select>

It would be a big help if you read the Stackoverflow instructions on inserting code into questions so it would format it correctly. Also the code you're showing somehow has a linked '1' stuck in the middle of it.
The error you're getting is because you have specified element :estrutura, :xpath, '//*[#name="_lyXWFMTGINID_ALAY"]' which means there should be only 1 of them. However on your page there are apparently 7 elements matching the xpath. Either make the selector more specific so it only matches one thing, or change it to elements :estrutura, :xpath, '//*[#name="_lyXWFMTGINID_ALAY"]' so it allows multiple elements to be found.
I would also suggest to stop using xpath when it's not necessary - elements :estrutura, '[name="_lyXWFMTGINID_ALAY"]'
Assuming you are meant to have multiple matching elements on the page and I'm reading you're code right then I think you'd be doing
screen_grupo_intervencao.estrutura[1].should have_text('CADASTRO TÉCNICO D&C - DISTRIBUIÇÃO E COLETA ENGENHARIA GESTÃO DE PERDAS GSC COMERCIAL GSO OPERACIONAL')
although that seems like a strange way to be testing.

Related

I can't get option value without using xpath in Cabybara

I'll try to explain again. I have to get the value Arizone but I'm only getting it via xpath. The element was mapped with: element :select_cad_state, "#uniform-id_state". I don't want to use xpath to get the value Arkansas, I want to use something like: select_state.send_keys(DATA[:cad_user][:_state]) ???..etc..etc..
I want to get the arizona value from the users.yml file and pass it as an argument in the front.
############ code page #####################
<div class="selector" id="uniform-id_state" style="width: 269px;"><span style="width: 259px; user-select: none;">Florida</span><select name="id_state" id="id_state" class="form-control" style="">
<option value="">-</option>
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">Arizona</option>
<option value="4">Arkansas</option></div>
################### my PageObjects #########################
class ScreenCadastro < SitePrism::Page
set_url 'http://automationpractice.com/index.php?controller=authentication&back=my-account'
element :input_cad_company, "#company"
element :input_cad_address, "#address1"
element :input_cad_city, "#city"
element :select_state, "#uniform-id_state" (my problem is here)
################ yaml file ##############
:cad_user:
:_password: 457226
:_company: SQATest
:_address: International Drive 678
:_city: Bradenton
:_state: Arizona
################## my env file ###################
DADOS = YAML.load(File.open(File.join(File.dirname(__FILE__) + "/massa/users.yml")))
input_cad_company.send_keys(DADOS[:cad_user][:_company])
input_cad_address.send_keys(DADOS[:cad_user][:_address])
input_cad_city.send_keys(DADOS[:cad_user][:_city])
find(:xpath,'/html/body/div/div[2]/div/div[3]/div/div/form/div[2]/p[7]/div/select/option[3]').click
You are asking about option value, but from all your code it looks like you actually want to select based on the string contents of the option element (not the value). As I posted in my answer to your previous question this should just be
select_state.select(DADOS[:cad_user][:_state])
If that is not working for you please provide the error message it's giving you.

How to get Error message when using pattern in angular 2

I have tried this code but it is not working.
<div>
<span *ngIf="!usernameRef.errors?.required">Amount</span>
<span *ngIf="!usernameRef.errors?.required">Cover amount required.</span>
<span *ngIf="usernameRef.errors?.pattern">Cover amount invalid.</span>
<input type="number" name="Amount" class="form-control" data-field="validate" placeholder="50 000" [(ngModel)]="Amount"
required #usernameRef="ngModel" pattern="[0-9]*" />
</div>
Your regex is also valid from two reasons:
Your input type is a number. You can't type letters.
[0-9]* - the asterisk means - 0 or more occurrences.

Read options from drop-down list into an array

I'm trying to write code (in Macro Express Pro) to read all of the options in a drop-down box and set them to an array in this format:
;option 1;option 2;option 3;option 4;option 5
So far I was able to make it display in a MsgBox (just as a test, I don't want the message box in production) ,but not append them to an array. Here is what I'm working with (from the site):
<select id="ctl00_cphContent_ddlWorkQueue" class="ddlbox">
<option value="4449">option 1</option>
<option value="4370">option 2</option>
<option value="4371">option 3</option>
<option value="4372">option 4</option>
<option value="4373">option 5</option>
</select>
From the VBS external script:
set OptionChooser = MyIE.Document.GetElementbyid("ctl00_cphContent_ddlWorkQueue")
For Each objOption in OptionChooser.Options
Msgbox objOption.InnerText
Next
Create a dynamic array and fill it with the options, then Join the array:
ReDim arr(OptionChooser.Options.Length - 1)
For i = 0 To OptionChooser.Options.Length - 1
arr(i) = OptionChooser.Options(i).Text
Next
str = Join(arr, ";")
MsgBox str

WebDriver Capture Text by XPath

I am attempting to capture a line of text for an automated WebDriver test to use it in a comparison later on. However, I cannot find an XPath that will work with WebDriver. I have used the text() function before to capture text that is not in a tag, but in this instance that is not working. Here is the HTML, note that this text will never be the same, so I cannot use contains or similar functions.
<div id="content" class="center ui-content" data-role="content" role="main">
<div data-iscroll="scroller">
<div class="ui-corner-all ui-controlgroup ui-controlgroup-vertical" data-role="controlgroup">
<a class="ui-btn ui-corner-top ui-btn-hover-c" style="text-align: left" data-role="button" onclick="onDocumentClicked(21228772, "document.php?loan=********&folderseq=0&itemnum=21228772&pageCount=3&imageTypeName=1003 Application - Final&firstInitial=&lastName=")" href="#" data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-theme="c">
<span class="ui-btn-inner ui-corner-top">
<span class="ui-btn-text">
<img class="checkMark checkMark21228772 notViewedCompletely" width="15" height="15" title="You have not yet viewed this document." src="../images/white_dot.gif"/>
1003 Application - Final. (Jan 11 2012 5:04PM)
</span>
</span>
</a>
In this example, the text I am attempting to capture is: 1003 Application - Final. (Jan 11 2012 5:04PM)
I have inspected the element with Firebug and I have tried the following XPaths with no success.
html/body/div[1]/div[2]/div/div/a[1]/span/span
html/body/div[1]/div[2]/div/div/a[1]/span/span/text()
The WebDriver test is being written in C#.
You can either use this
driver.FindElement(By.XPath(".//div[#id='content']/following-sibling::span[#class='ui-btn-text']")
or
var elem = driver.FindElement(By.Id("Content"));
string text = string.Empty;
if(elem!=null) {
var textElem = elem.FindElement(By.Xpath(".//following-sibling::span[#class='ui-btn-text']"));
if(textElem!=null) text = textElem.Text();
}
I was able to solve this issue by removing the span tags from the XPath.
GetText("html/body/div[3]/div[2]/div/div/a[1]", SelectorType.XPath);
python webdriver code looks something like
driver.find_element_by_xpath("//span[#class='ui-btn-text']").text
But locator may be not uniqe, because I can't see all the code
PS Try to never use locators like html/body/div[1]/div[2]/div/div/a[1]/span/span
Approach:
Find the CSS Selector from the Given DOM
Derived CSS:css=#content div.ui-controlgroup > a[onclick*='onDocumentClicked'] > span > span
Use the C# Library Method to get the Text.

Locating element in same paragraph of another element in watir-webdriver

Given the following HTML code snippet; after finding the link by ID, how would you select the checkbox in the same paragraph?
For example if I wanted to select the checkbox associated with the link with ID="inst_17901-1746-1747".
The order of the paragraphs in the DIV is not consistent between sessions so I cannot select it by index or ID of the checkbox.
<div id="inst-results">
<p>
<input id="inst-results0-check" type="checkbox">
<a class="ws-rendered" id="inst_17901-1746-1747" title="!!QA Data 2/DOOR FURNITURE/316 Stainless - Altro Range"><img src="http://yr-qa-svr2/Agility/ACMSImages?type=objectType&objectTypeID=32"> <span>!!QA Data 2/DOOR FURNITURE/316 Stainless - Altro Range</span></a>
</p>
<p>
<input id="inst-results1-check" type="checkbox"><a class="ws-rendered" id="inst_17882-1746-1747" title="!!QA Data/DOOR FURNITURE/316 Stainless - Altro Range"><img src="http://yr-qa-svr2/Agility/ACMSImages?type=objectType&objectTypeID=32"> <span>!!QA Data/DOOR FURNITURE/316 Stainless - Altro Range</span></a>
</p>
</div>
I figured out this solution working off the text of the link, but Zeljko solution is much better.
$browser.div(:id,"inst-results").ps.each { |para|
if para.link.text == "!!QA Data/DOOR FURNITURE/316 Stainless - Altro Range" then
para.checkbox.set
break
end
}
If there is only one checkbox in the paragraph with the link:
browser.link(:id => "inst_17901-1746-1747").parent.checkbox.set
Works with watir-webdriver, not sure if it would work with other Watir gems.

Resources