using element_name text in rspec match when_visibile - watir-webdriver

I have a page-object where i define an element as
class ProjectCreate
include PageObject
div(:feedback, :class => 'feedback_valid')
button(:save, :text => 'Save', :index => 0)
end
I'm using rpsec and i'm attempting to wait for feedback_element to be visible and then use the text from the div element in my match. Here is my test..
describe 'Add Project' do
it 'Provided Form Sucessfully' do
on(ProjectCreate) do |page|
page.set_client('add_project/valid')
page.save
expect(page.feedback_element.when_present.feedback).to match /Form successfully saved/
end
end
However I'm getting this error.
NoMethodError: undefined method `feedback' for #<Watir::Div:0x3b86f50>
I thought that by calling div element by name it would return the text and I could use that in my expect match.
Any thoughts on where I'm going wrong?

The when_present method returns the element that it as waiting for. The element does not have the feedback method; it is the page-object that does.
You can either get the text of the element that is returned by when_present:
expect(page.feedback_element.when_present.text).to match /Form successfully saved/
Or you can wait for the feedback element and then check the value of the feedback text:
page.feedback_element.when_present
expect(page.feedback).to match /Form successfully saved/

Related

How Can I Verify Text Amount in Katalon

I'm trying to verify a text in Katalon and my script isn't working.
Here's my element:
<span id="overviewTabStoreCredit" class="h2 strong amountCredit text-danger">-$100.00</span>
Here's my script:
def StoreCreditAmount = '-$100.00'
TestObject StoreCreditTO = findTestObject('Baseline/Page_Side Menu/Page_Customers/Page_Customer Card/span_Verify Credit Limit')
WebUI.verifyElementAttributeValue(StoreCreditTO, 'text', StoreCreditAmount, GlobalVariable.G_Timeout_Tiny, FailureHandling.CONTINUE_ON_FAILURE)
When running the script, I get an error message, "Object does not have attribute 'text'"
I also tried this to character it by class instead of text:
def StoreCreditAmount = 'h2 strong amountCredit text-danger'
TestObject StoreCreditTO = findTestObject('Baseline/Page_Side Menu/Page_Customers/Page_Customer Card/span_Verify Credit Limit')
WebUI.verifyElementAttributeValue(StoreCreditTO, 'class', StoreCreditAmount, GlobalVariable.G_Timeout_Tiny, FailureHandling.CONTINUE_ON_FAILURE)
I got this error:
Has attribute 'class' with actual value 'text-success h2 strong amountCredit' instead of expected value 'h2 strong amountCredit text-danger' even though the value is correct.
'Text' might not be an attribute. You can getText() from the element and then compare with the expected result. Sometimes, the value you see might not from Text, but from the attribute 'value'.
When you look at your tag there is no "text" attribute:
<span id="overviewTabStoreCredit" class="h2 strong amountCredit text-danger">
Some elements (like text-boxes) have hidden "value" elements for input text, but that is not the case here.
I believe what you want to do is check that the text between your tags equals a certain amount, in this case: "-$100.00".
To check the text between your opening/closing tags for your element use
WebUI.getText(). So your code could grab the text between the tags of your element, and then do an assert (or do it in one step) to finish your validation. I'll show it in two for readability:
def testStoreCreditAmountText = '-$100.00'
TestObject storeCreditTO = findTestObject('Baseline/Page_Side Menu/Page_Customers/Page_Customer Card/span_Verify Credit Limit')
def actualStoreCreditAmountText = WebUI.getText(storeCreditTO)
WebUI.verifyMatch(testStoreCreditAmountText, actualStoreCreditAmountText, false)
I hope that helps!

Date validation for custom field - SuiteCRM Version 7.10.4 Sugar Version 6.5.25 (Build 344)

I have two fields in my module called: start_date_c & end_date_c with date datatype
These fields are not mandatory fields however when i enter data into the end_date_c field I would like to make it is not less than start_date_c
I have tried the following:
https://suitecrm.com/suitecrm/forum/suitecrm-7-0-discussion/12522-how-to-validate-start-and-end-date-in-suitecrm
http://sugarmods.co.uk/how-to-add-custom-validation-to-form-fields-in-sugarcrm/
but as i am new to suiteCRM, i am not able to find positive response
You will need 2 things
Edit the file editviewdefs.php in the module you want to add the logic. This field will be autogenerated when you add the first custom field to the edit view.
Create your custom JS logic to define when the field is valid.
This logic here will add a validation callback for your
addToValidateCallback(
'EditView', // Form Name
'end_date_c', // field name
'datetime', // Field type
false, // Is required
"End date cannot be earlier than start date", // Message
function() {
//WRITE YOUR JS VALIDATION HERE, return true when is valid
});
In the editviewdefs.php find the field definition and use the displayParams to make suite/sugar add the JS for you.
array (
'name' => 'end_date_c',
'displayParams' =>
array (
'updateCallback' => 'FUNCTIONNAME',
),
),
The last step ain't needed if you already have a global custom JS (like style.js file for a custom theme).
EDIT: javascript DisplaParams will not work, so added the updateCallback option.
Now this validation works in 2 ways.
The updateCallback will be fired onChange
The addtoValidateCallback will be fired on Save.
This will give you enough flexibility to validate the form.
Simple and one linear, try following in any JS file (added in module edit view):
addToValidateDateBefore('EditView', 'start_date_c', 'date', false,'Date Start', 'end_date_c' );
worked for me.I added the following code to the fields in modules/custom_module/vardefs.php
'audited' => true,
'enable_range_search' => true,
and added the following to the start field
'validation' =>
array (
'type' => 'isbefore',
'compareto' => 'enddate',
'blank' => true,
),

look_down that checks (inside HTML) content

With HTML::TreeBuilder, using command $root->look_down(_tag => 'a') I get first anchor.
(1) How can I find last anchor?
Additionally, how can I check for inside content of the tag, to check if it does or does not contain some string inside of it? So for example,
(2) how can I find an anchor that contains "Hallo" or "hallo" in inside HTML?
(3) how can I find an anchor that DOES NOT contain "Hallo" or "hallo" in inside HTML?
The look_down() function returns a list of all <a> tags found, so simply access to last element of it using an index, like:
my $last_a_tag = ($root->look_down(_tag => 'a'))[-1]
To search into its text, use content_list() function, that returns a list with all child text elements. Then use a map() function to check if it contains or not any text, like:
map { m/[Hh]allo/ } $last_a_tag->content_list;

Selecting elements inside a frame with a dynamic src and NO id

I am trying to select a value in a dropdown which is inside a iFrame. The frame contains no id but has a src that is dynamic.
/MyApplicantPortal/Applicant/244029/SelectOrderTemplate?t=1
HTML:
<html class = ......>
.
.
.
the number 244029 changes and because of this, I am not able to identify the objects inside the frame. The iframe is the child of the div.
My code is
in_frame(:src => 'MyApplicantPortal/Applicant/244025/SelectOrderTemplate?t=1') do |frame|
select_list(:template, :id => 'selectlisname', :frame => frame)
end
Solution 1 - Using a regexp
Contrary to my initial assumption, the in_frame method does not support locating a frame using regular expressions (Issue 197). However, it is possible to monkey patch the functionality in. Where you require page-object, add the following code to monkey match the nested_frames method.
require 'watir-webdriver'
require 'page-object'
module PageObject
module Platforms
module WatirWebDriver
class PageObject
def nested_frames(frame_identifiers)
return if frame_identifiers.nil?
frame_str = ''
frame_identifiers.each do |id|
value = id.values.first
if value.is_a?(Regexp)
frame_str += "frame(:#{id.keys.first} => #{value.inspect})."
else
frame_str += "frame(:#{id.keys.first} => #{value})." if value.to_s.is_integer
frame_str += "frame(:#{id.keys.first} => '#{value}')." unless value.to_s.is_integer
end
end
frame_str
end
end
end
end
end
This then allows you to use a regular expression to define the src of the frame (when using in_frame):
class MyPage
include PageObject
in_frame(:src => /MyApplicantPortal\/Applicant\/\d+\/SelectOrderTemplate\?t=1/) do |frame|
select_list(:template, :id => 'selectlisname', :frame => frame)
end
end
Note that in a regexp, you need to escape the characters \*?{}.. The \d+ part means that any number will be matched.
Solution 2 - Define a frame_element
If you do not want to monkey patch the page object gem, then you could:
Define the frame element (using the regular expression)
Use a block, which references the frame element, to define the select list
This would look like:
class MyPage
include PageObject
element(:select_order, :frame, :src => /MyApplicantPortal\/Applicant\/\d+\/SelectOrderTemplate\?t=1/)
select_list(:template){ select_order_element.select_list_element(:id => 'selectlisname') }
end

Zend Avoid submit button value in GET parameters in url

I have a form, created with Zend_Form, with method = GET used for searching records with elements as below:
[form]
user name [input type="text" name="uname"]
[input type="submit" value="Search" name="search"]
[/form]
After form is submitted all the GET parameters along with submit button value are appearing in the url.
http://mysite.com/users/search?uname=abc&search=Search
How to avoid submit button value appearing in the url? is custom routing the solution ?
When you create your element, you can simply remove the name attribute that was automatically set at creation
$submit = new Zend_Form_Element_Submit('search')->setAttrib('name', '');
Or inside a Zend_Form
// Input element
$submit = $this->createElement('submit', 'search')->setAttrib('name', '');
// Or Button element
$submit = $this->createElement('button', 'search')->setAttribs(array
(
'name' => '', 'type' => 'submit',
);
When a form gets submitted, all of its elements with their names and values become a part of a GET / POST - query.
So, if you don't want an element to appear in your GET - query, all you need to do is to create this element without a name. That's probably not the best approach, but since we're talking about the 'submit' element, I guess it doesn't matter that much.
Looking at Zend_View_Helper_FormSubmit helper, you can see that it's creating the 'submit' element and setting its name. So, the possible solution would be to create your own view helper and use it for rendering the 'submit' element instead of the default helper.
You can set a custom helper with
$element->setAttribs( array('helper' => 'My_Helper_FormSubmit') );
Then build your own form element class and remove the name attribute from the element with preg_replace. The beauty of it is, it will not interfere with the other decorators.
So the something like this:
class My_Button extends Zend_Form_Element_Submit
{
public function render()
{
return preg_replace('/(<input.*?)( name="[^"]*")([^>]*>)/', "$1$3", parent::render(), 1);
}
}
You can remove name attribute for submit button in javascript.
jQuery example:
$('input[name="submit"]').removeAttr('name');
In the controller that represents the form's action, redirect to another (or the same controller) only including the relevant params.
Pseudocode:
$params = $this->getRequest()->getParams();
if isset($params['search'])
unset($params['search']);
return $this->_helper->Redirector->setGotoSimple('thisAction', null, null, $params);
handle form here
This is basically the same idea as Post/Redirect/Get except that you want to modify the request (by unsetting a parameter) in between the different stages, instead of doing something persistent (the images on that Wiki-page shows inserting data into a database).
If I were you, I would leave it in. IMO it's not worth an extra request to the webserver.