Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara testing if page has regular expression

I'm new to RSpec and Capybara and I am trying to test if page has a time on it. 12:00 or 14:24 or 08:12 are good examples of what I am trying to test for.

I am using the following regex:

/^\d{2}:\d{2}/

I am not sure how to "phrase it" in capybara to test if the page contains this regex.

like image 937
Marius Pop Avatar asked Sep 20 '12 12:09

Marius Pop


People also ask

How do I test regular expressions in Pega Platform?

Click Test Expression. Pega Platform generates a table that lists the results of each match of regular expressions found in the source text. Did you find this content helpful? Have a question?

How do I run a test using a regular expression?

Click ActionsRun. The system displays a test input form. On the test input form, in the Page list, click Empty test page. Leave the parameter values blank. Click Run. Enter a regular expression to use, using syntax conforming to the Java implementation of regular expressions ( Java.util.regex.Pattern ).

How do I empty a test page in code-Pega-parse?

Use the Application Explorer to open the Code-Pega-Parse.RegExpTester standard activity. Click ActionsRun. The system displays a test input form. On the test input form, in the Page list, click Empty test page. Leave the parameter values blank. Click Run.

How do I use regular expressions in Java?

Enter a regular expression to use, using syntax conforming to the Java implementation of regular expressions ( Java.util.regex.Pattern ). Enter source text to use to search for matches to the regular expression.


2 Answers

I'd suppose something like

page.text.should match(/^\d{2}:\d{2}/)

or, with the new RSpec syntax

expect(page.text).to match(/^\d{2}:\d{2}/)

Or you can test not the whole page but some element, e.g.

find("span.time").text.should match(/^\d{2}:\d{2}/)
like image 168
khustochka Avatar answered Sep 20 '22 19:09

khustochka


I don't know if this is new behavior, but you can just use has_content?:

page.has_content?(/do not have permission/i)

In rspec, that would be something like this:

expect(page).to have_content(/do not have permission/i)

I found this in the online documentation for capybara, but it's under has_text? since has_content? is just an alias.

like image 31
Nerdmaster Avatar answered Sep 23 '22 19:09

Nerdmaster