How to use expect in text with line breaks? - cucumber

I'm doing some automated tests with capybara, cucumber and webdriver.
And I have an objective to know if a text was loaded on a page, however there is a css property in it white-space that breaks the text and the expect cannot find.
My code is like this.
HTML
<p style="white-space: pre-wrap;">
Some
cool
text
</p>
Cucumber.Feature
expect(page).to have_selector('p', text: 'Some')
expect(page).to have_selector('p', text: 'cool')
expect(page).to have_selector('p', text: 'text')
I'm looking for something similar to:
#This text will failure in teste
expect(page).to have_selector('p', text: 'Some cool text')

Capybara tries to treat text the way the browser displays it. If the browser is displaying the text on multiple lines then try
expect(page).to have_selector('p', text: "Some\ncool\ntext")
If that doesn't work you can always use a regex along the lines of
expect(page).to have_selector('p', text: /Some\s+cool\s+text/)

Related

Can i use {{sometitle}} as text (without parsing as chunk) in modx evo?

I just got a problem using revolution slider in modx evolution.
My slider have jquery code
tabs: {
tmp:'<div class="tp-tab-content"> <span class="tp-tab-date">{{param1}}</span> <span class="tp-tab-title">{{title}}</span></div><div class="tp-tab-image"></div>'}
and modx of course parsing {{param1}} and {{title}} as chunks, but its part of revoslider's jquery.
so after parsign page html code looks like this
tabs: {
tmp:'<div class="tp-tab-content"> <span class="tp-tab-date"></span> <span class="tp-tab-title"></span></div><div class="tp-tab-image"></div>'}
and revolution slider shows incorrect
i tried calling revoslider code from html file using snipnet with php funtion include, but it also got parsed by modx engine
can i solve this problem without changing revoslider jquery code?
thanks to your ansewers!
I am not an EVO expert, but I could suggest several tricks that may help:
Try to escape the curly braces like \{\{param1\}\}
Try to breake the sequence like {[*fakeTemplateVar*]{param1}[*fakeTemplateVar*]}*
Try an empty TV like [*fakeTemplateVar*:ifempty={{param1}}]
*fakeTemplateVar will finally evaluate to an empty place in the template.

Selenium Webdriver: find a span that has an inner span with specific text

Using Selenimum Webdriver in Python, I want to be able to find My Text from the following html code:
<span class="first">My Text
<span class="second">(Total)</span>
</span>
The inner span has a unique word in it which is (Total).
The outer span has the text that I am looking for, which is My Text. How can I find this text?
I have tried to use something like:
driver.find_elements_by_xpath("//*[contains(text(), '(Total)')]/prededing-sibling::span")
But it was not successful.
I appreciate any help on this.
Try below solution to get required text:
span = driver.find_element_by_xpath('//span[span="(Total)"]')
required_text = driver.execute_script('return arguments[0].childNodes[0].textContent', span).strip()
Output of print(required_text)
'My Text'
Note that span with text "My Text" is not a sibling of span with text "(Total)", but its parent, so you can not fetch it with preceding-sibling::span
You are using preceding sibling, that's wrong, you are looking at what's there in the parent node
So use this
"//span[text()='(Total)']/.."
The above xpath helps you to locate the corresponding parent node, and then you use programming language to get the particular string
I don't know Python, I would do in Ruby in the following way
a='(Total)'
puts b.element(xpath: "//span[text()='#{a}']/..").text.chomp(a).strip
=> My Text

JSP out.println not rendering html tags

In a text area in jsp i am trying to dipslay some text in bold but it is not rendering the tags and displaying as it is:
e.g.
<% out.println("<b>"+Pattern+"</b>");%>
where String Pattern =ERROR
this shows as
<b>ERROR</b> .....in the veiw source it shows like <b>ERROR</b>
Is it not supposed to work this way ? what are other ways to make it bold.
Change it to:
<%= Pattern %>
its like printing it direct into the html Page
Just try <b><% out.println("+Pattern+");%></b>.

Dynamic database search result on a website

This question regards website programming. My primary languages are c++/c# and I don't know much about web development, except that say I understand html and css. That's why I'm looking for a relatively simple solution, preferably something out of the box. I don't have any experience with JavaScript, but for the sake of this project I'm willing to learn it if necessary.
Let's say I have a database, where each entry is about a book. It contains the fields: title, author(s) and publication date.
I would like to create a simple website with a search box that has this dynamic result feature, so that you get suggestions after you type in a few letters. All those suggestions, as well as search results, need to be based purely on the database.
This could be a static website or based on any Content Management System, I'm familiar with Joomla, but was unable to find an out-of-the box component that would do just that. All those search modules search the entire website and I only need to search the database.
Probably I can help you with how to implement this feature. We used to call this feature as autocomplete menu.
First you decide minimum characters to populate autocomplete menu. For example 2
By using javascript you write keyup event. Once the characters count reaches to the minimum character count. You send AJAX request to the server.
The server should process this request and do the database search and form a json or xml response or plain text to the client.
The client parses that response into javascript object and construct a dynamic html for autocomplete menu with the data and render it into the DOM hence display's just below the search text box.
Now if you want to display the first result inside the textbox as you type. Here is the method I can suggest as similar as google search box
Place one label or span just below the input text box. By using css make its position exactly match with the position of the text box. Make sure the starting of text of both input text and label matches. Make the text color of the label less brighter than the font color of text. The font size and font family of the label should match the input text field's style. Now by using Javascript display the first or most matched text inside the label. Please find the sample code below
<!DOCTYPE html>
<head>
<style>
label {
position:relative;
}
body, input {
font-family: 'verdana';
font-size: 12px;
}
</style>
</head>
<html>
<body>
<form action="demo_form.asp">
<input type="text" name="fname" placeholder="Matched Text" /><br>
<label> Matched Text</label><br>
</form>
</body>
</html>

How do I get at image title attributes in Watir?

Given the following HTML code (which, I realise, sucks, but that's not something I can currently solve):
<img height="64" width="64" class='list_item' src="/img/icon/first.jpg"
title="This is the first item::Completed the item "I did this first"" alt="First" />
gives me a result of (this is an image.to_s)
name:
type:
id:
value:
disabled:
src: /img/icon/first.jpg
width: 64
height: 64
alt: First
Note lack of "title" element. This does not actually change (the lack of the title element)
If I get the contents of the parent div of one of those icons, I get something like:
<img class="list_item" I="" did="" this="" first="" src="/img/icon/first.jpg" alt="First">
The broken HTML of the original has been turned into separate attributes somewhere down the line, but the title tag appears to have been stripped completely, and since it's the contents of the title tag I need, I'm a little stuck.
This has been tried with lastest Watir on Ruby 1.9.2 using Firefox.
Perfect world solution: I'd like to get the original transmitted HTML for the image tag, so I can "special case" (ie, hack) around the stupid double-quote problem.
Good Enough Solution: the contents of the title tag.
There is actually a #title method on Watir::Image. With the above incorrect HTML the output would be like this (where 'i' is the Image object):
i.title
=> "This is the first item::Completed the item "
This shows only part of the title.
But you could use #html and then parse all the necessary information out of it with some magic:
i.html
=> "<IMG class=list_item title=\"This is the first item::Completed the item \" alt=First src=\"/img/icon/first.jpg\" width=64 height=64 first?? this did I>"
But as other answers above have mentioned - you cannot get it out correctly due to the bad HTML. Maybe there's some other way to accomplish your bigger goal you're having?
getting the title probably isn't working because the way the title attribute is set on that element isn't valid. entities " and < and > need to be escaped inside html attributes, with " and < and > respectively. Escape the quotes and try again.
Not sure, but I don't think Watir supports image titles. I looked over the Supported Elements page, title was x'ed out. I don't see it in the RDoc for Watir::Image type either.

Resources