testing a body contains with space - testing

I would like to write a phpunit test that must check for the presence of two elements in body, i need to check 2 different synchronized-time ; eg: <small>12/02/2022 00:00</small>
The first element is written in body on one line <small>12/02/2022 00:00</small>, so the assert is correct but the second element poses a problem because the code is rendered with spaces ,so the 2nd test returns false : this is the html code in a body :(<i class="fas fa-clock"></i> 12/02/2022 00:00 </small>)
this is my assert : $this->assertResponseContains(('<i class="fas fa-clock"></i> 12/02/2022 00:00 </small>'),null, false);
to ingnoring Case but the response still false ..
sorry for my bad English

Related

Check if an input field is empty or not is not working properly in Cypress tests

I got 2 step definitions in Cypress that check if an input field is empty or not (depends on how I build the sentence I setup with RegEx).
First my problem was, that cypress said the test failed because the input field is empty while it was not.
My defined steps:
/** We check if the input field with the given name is empty */
Given(/^The input field "(.*)" is (not )?empty$/, (inputFieldName, negation) => {
if (negation === 'not ') {
CypressTools.getByName(inputFieldName).should('not.be.empty');
} else {
CypressTools.getByName(inputFieldName).should('be.empty');
}
});
/** We check if the input field with the given name is visible and empty */
Given(/^The input field "(.*)" is visible and empty$/, (inputFieldName) => {
CypressTools.getByName(inputFieldName).should('be.visible').should('be.empty');
});
In my specific test cypress should check a value filled input field and the step is defined like that:
The input field "XYZ" is not empty
I can see, that the if-condition is working fine, so no problems on the definition or RegEx site.
But the test fails because Cypress say the input field is empty but it's not.
I suspect, that Cypress test the input fields for values between the input tags, but doesn't check for a value attribute in the input tag.
At least, I tried to add an invoke('val') in the step definition:
CypressTools.getByName(inputFieldName).invoke('val').should('not.be.empty');
and it works for the first step definition, but when I do that for the 2nd one aswell, cypress tests fail and tell me this:
Timed out retrying: You attempted to make a chai-jQuery assertion on an object that is neither a DOM object or a jQuery object.
The chai-jQuery assertion you used was:
> visible
The invalid subject you asserted on was:
>
To use chai-jQuery assertions your subject must be valid.
This can sometimes happen if a previous assertion changed the subject.
I don't understand the problem here. Is this method valid with invoke() or is there a better solution to cover all cases?
Thanks a lot for your help.
The problem your error message is pointing to is that the subject being passed along the command chain in not appropriate for the next step,
CypressTools.getByName(inputFieldName)
.invoke('val') // changes subject to the text of the input
// (not a DOM element)
.should('be.visible') // needs a DOM element
.should('not.be.empty');
The surest way around it is to break the testing into two steps
CypressTools.getByName(inputFieldName).should('be.visible');
CypressTools.getByName(inputFieldName)
.invoke('val')
.should('not.be.empty');
but I think a simple reordering will also work
CypressTools.getByName(inputFieldName)
.should('be.visible') // check the DOM element, passes it on as subject
.invoke('val') // changes subject to the text of the input
.should('not.be.empty'); // check the text is not empty

Require help in identifying element in testcafe

I am new to automation testing and coding. I am using testcafe in one of my project to automate the functional testing.
In one of the webpage there is a field which only accept numeric values and gives error message if any alphanumeric values are entered.
As a part of my validation I need to capture this error message.
Issue which I am facing here is I am not able to determine which element it is in DOM.
For example gmail user name can be considered for this and error message that we get while we try to enter invalid user message.
This is how the DOM looks like for the field
<div class="flex-content space-100 space-large-reset ">
<label for="uid">Unique ID Number</label>
<input type="password" id="uid" maxlength="9" value="123js" class="abyss-textinput abyss-form-invalid">
<div class="abyss-error-message">Please enter a valid Unique ID Number.</div></div>
Value ="123js" is the incorrect value which i entered which generated error message mentioned in next line.
Thanks in advance.
Firstly, I'll confess I'm no authority on testcafe, just learning myself.
That being said I've assertions which work as follows:
await t.expect(Selector('h2').withText('Some text').exists).ok();
In you case if there's no id/class you can use to narrow the search it could be fairly inefficient (there are likely a lot of divs/spans in your app). Could you provide some info about the dom used to display the errors ?
As a #Iain_b said, you can use a Selector and the ok() assertion to check presence of the error message. You can use Adjacent sibling combinator to select the error's <div> tag. However the element can be inserted into the DOM tree after validation is failed, or be there since page loading and just become visible after validation is failed.
So for the first case you should use something like:
await t.expect(Selector('#uid + .abyss-error-message').withText('Please enter a valid Unique ID Number').exists).notOk();
await t.typeText('#uid', '#$dfgh');
await t.expect(Selector('#uid + .abyss-error-message').withText('Please enter a valid Unique ID Number').exists).notOk();
and for the second:
await t.expect(Selector('#uid + .abyss-error-message').withText('Please enter a valid Unique ID Number').visible).notOk();
await t.typeText('#uid', '#$dfgh');
await t.expect(Selector('#uid + .abyss-error-message').withText('Please enter a valid Unique ID Number').visible).notOk();
Thanks for the help guys..
I was able to find the element using below mentioned condition
Selector ('.abyss-error-message').withText('Please enter a valid UID')

rebol parsing html: get error "title has no value"

I'm trying to parse an html page:
url: https://dzone.com/articles/2-entity-framework-alternatives-or-give-me-data
html: read url
parse html [
to {<h1 class="article-title" itemprop="headline">}
thru {<h1 class="article-title" itemprop="headli
ne">}copy title to {</h1>}
]
probe title
Can't see why it doesn't work since I get error "title has no value"
I assume that you're using Rebol/view since the free versions don't do https though rebol3 does.
If you want to see if something is working you should look at the return value of the parse, and you'll see it's false which means that there's a problem with your parse rule. Anyway, this works for me though the quotes around the tags are not necessary as < and > are both string delimiters.
>> parse html [
thru <h1 class="article-title" itemprop="headline">
thru <h1 class="article-title" itemprop="headline">
copy title to </h1> to end
]
== true
>> trim/head/tail title
== "2 Entity Framework Alternatives (or Give Me Data!)"
It does not work most probably because the first to stops before the matched string, so that thru starts at the beginning of the first occurence of <h1 ...>, not at the second as you might have expected. You need to skip the first occurrence, before trying to search for the second one. You can achieve that using two thru rules as shown in another answer, or just repeat the rule twice to avoid duplicating it:
parse html [
2 thru <h1 class="article-title" itemprop="headline">
copy title to </h1> to end
]
Notice the final to end rule, which will make parse return true if your rules succeed in reaching the end. The to end rule is a placeholder rule, as you do not care about what is following </h1>, but want to reach the end of the input anyway.
EDIT: Testing the code you submitted works fine from here unchanged. The editing of your question has actually fixed the cause of the error. I can reproduce your issue with your original code.

Selenium WebDriver sendkeys type incorrect value to input field if value that typing contains forward slash (/)

Selenium WebDriver sendkeys method sometimes type incorrect value to text area of input field, if value that typing contains forward slash (/) character.
For examlple
element located in:
<input id="dp1416998181403" class="single-value hasDatepicker" type="text" maxlength="10" value="12/31/2010" style="width: 67px;">
code
String a = "12/31/2014"
driver.findElement(By.id("dp1416998181403")).sendKeys(a);
in some case actual value that typed is equals: 31/2014 instead of 12/31/2014
or 2014 instead of 12/31/2014
This problem isn't reproduce always
I noticed, in some case, Selenium type first 3 character (12/), then delete them and type rest of character (31/2014)
Has anybody have the same problem?
I found a problem with typing a /, but problem was that / changing to 7 when typing
#user3293822 :
Not Really But can you try to add explicit wait before you send keys , Here is an example of what you can do..I am sure you will not find any data loss during execution.
String a = "12/31/2014";
WebDriverWait Test_element = new WebDriverWait(driver, 10);
Test_element.until(ExpectedConditions.elementToBeClickable(By.id("dp1416998181403"))).sendKeys(a);
Try the above and let me know what happens

How to get All Text in robot framework ?

Consider the following source code,
<div id="groupContainer" class="XXXXXX">
<ul id="GroupContactListWrapper" class="list-wrapper">
<li class="contactNameItemContainer">
<div class="contactNameItem">
<span class="name">Name1</span>
</div>
</li>
<li class="contactNameItemContainer">
<div class="contactNameItem">
<span class="name">Name2</span>
</div>
</li>
</ul>
</div>
How do i retreive the two names (Name1,Name2) in a list variable ?
I tried the following xpath for a "Get Text" keyword, but only returns the first one.
//div[#id='groupContainer']//li[#class='contactNameItemContainer']//span
Please suggest
You could iterate over the elements as below:
${xpath}= Set Variable //div[#id='groupContainer']//li[#class='contactNameItemContainer']//span
${count}= Get Matching Xpath Count ${xpath}
${names}= Create List
:FOR ${i} IN RANGE 1 ${count} + 1
\ ${name}= Get Text xpath=(${xpath})[${i}]
\ Append To List ${names} ${name}
This works but is rather slow when there are many matches.
You could extend Selenium2Library and write your own keyword for this purpose. Save the following as Selenium2LibraryExt.py
from Selenium2Library import Selenium2Library
class Selenium2LibraryExt(Selenium2Library):
def get_all_texts(self, locator):
"""Returns the text value of elements identified by `locator`.
See `introduction` for details about locating elements.
"""
return self._get_all_texts(locator)
def _get_all_texts(self, locator):
elements = self._element_find(locator, False, True)
texts = []
for element in elements:
if element is not None:
texts.append(element.text)
return texts if texts else None
Then you can use your new Get All Texts keyword in your tests like this:
*** Settings ***
library Selenium2LibraryExt
*** Test Cases ***
Get All Texts Test
Open Browser http://www.example.com chrome
#{texts} Get All Texts css=.name
Log Many ${texts}
Though the top-rated answer is fully working - and the most xpath-ish :), let me add an option I don't see proposed yet - using the Get Webelements keyword. E.g.:
#{locators}= Get Webelements xpath=//div[#id='groupContainer']//li[#class='contactNameItemContainer']//span
${result}= Create List
:FOR ${locator} in #{locators}
\ ${name}= Get Text ${locator}
\ Append To List ${result} ${name}
It'll generate and return a list of all matching elements, on which you just iterate on. It might be a tad faster than xpath's [index] reference because the dom is evaluated once - but don't hold me accountable if that's not fully true :)
Get Text will return content of the first element that matches the locator. When using XPATH you can specify the index of the element you want to get, like this:
${name1} Get Text xpath=//div[#id='groupContainer']//li[#class='contactNameItemContainer'][0]//span
${name2} Get Text xpath=//div[#id='groupContainer']//li[#class='contactNameItemContainer'][1]//span
#{names} Create List ${name1} ${name2}
#Velapanur
I had another similar requirement where in i have to enter the texts into textareas in a page.
The below i wrote with the idea in reference to what Todor had suggested, and it worked. Many thanks to Todor, Velapanur
#{Texts}= Get WebElements ${AllTextboxes}
:FOR ${EachTextarea} in #{Texts}
\ Input Text ${EachTextarea} ${RandomTextdata}
Here is the logic to get all elements in Java. You can adopt it to your need.
You have to use findElements() and not findElement() to get all elements.
List<WebElement> items = driver.findElements(By.cssSelector("ul#GroupContactListWrapper div.contactNameItem"));
foreach(item in items){
System.out.println(item.getText();
}
If you want a particular element from the list you can use items.get(1)