Checking for a table row using DomCrawler - symfony

I am writing a phpunit test... on my page I have several rows, one of them is like this:
<tr><td>MATCH<small><span class="glyphicon glyphicon-pushpin"></span></small></td></tr>
Some are like this:
<tr><td>NOT A MATCH 1</td></tr>
<tr><td>NOT A MATCH 2</td></tr>
<tr><td>NOT A MATCH 3</td></tr>
how can I run a test to check that the row with the pushpin glyphicon is the one with "MATCH"?
Basically I want the test to confirm that the glyphicon is appearing on the correct row, and just having something like $crawler->filter('small:contains("' . $glyphCode . '")')->count() only confirms that the glyph exists - not that it's in the right place.
Any help appreciated, thank you.

You can use XPath Selectors with Symfony's DomCrawler.
To select your desired element use this XPath expression:
//td//small/span[#class="glyphicon glyphicon-pushpin"]
Then place it inside a PHPUnit assertion.
$this->assertEquals(1,
$crawler->filterXPath('//td//small/span[#class="glyphicon glyphicon-pushpin"]')->count()
);
I've used assertEquals 1 as expected value, to ensure that one element is found on the page.

Actually, the question can be treated as a string match problem.
There are several different ways to do that.
use PHP Simple HTML DOM Parser
$ret = $html->find('td[class=*glyph]');
use regular expression in PHP
the pattern string may like /class="[^"]+glyph/
run grep command in the shell
$ grep glyph xxx.php

Related

Scss classnames with special symbols like "[]"

This is a strange question. I request link type from server which might return "[TCP]" or "[UDP]". And I wanna use the string as classname directly for different background color like. What I want is :
<div className={`${styles.span} ${styles["[TCP]"]}`}/>
But the css selector ".[TCP]" is not allowed, below error given:
SassError: Invalid CSS after "&": expected selector, was ".[TCP]"
Now I am using .replace(/\[|\]/g,"") split the string "[TCP]" --> "TCP". But I hope someone can tell me another way or it's impossible.
"You can use [TCP] as classname."
As written here (demo) you can use any character for classname except NULL. All you have to do is in CSS write \ before special characters. In your case, it would look like this .\[TCP\].
But I believe it's much easier to just remove the special characters.

How to access map in template string?

I want to use values from gradle.properties that should go into a template string.
A naive first:
println("${project.properties[somekey]}")
doesn't work: Unresolved reference: somekey
So, quotes required?
println("${project.properties[\"somekey\"]}")
is completely broken syntax: Expecting an expression for the first .
I couldn't find any example how to do this, yet the official documentation says expressions.
Question: is it possible to access a map in string template, and if so, how?
Yes and as follows:
"${project.properties["someKey"]}"
assuming the Map has the following signature: Map<String, Any?> (or Map<Any...)
Alternatives:
"${project.properties.getValue("someKey")}"
"${project.properties.getOrElse("someKey") { "lazy-evaluation-default-value" }}"
"${project.properties.getOrDefault("someKey", "someFixedDefaultValue")}"
Basically all the code you put in the ${} is just plain Kotlin code... no further quoting/escaping required, except for the dollar sign $ itself, e.g. use "\$test" if you do not want it to be substituted with a variable named test or """${"$"}test""" if you use a raw string
Note that in this println case the following would have sufficed as well (which also goes for all the shown alternatives above. You may omit the outer surrounding quotes and ${} altogether):
println(project.properties["someKey"])
See also Basic types - String templates

Using xpath to match a string that contains any integer (between 0 and 9)

I am new to XPath and need some help.
The system auto generates the id which looks something like this:
<input type="file" class="form-file" size="22"
name="files[entry-23245_field_entry_attachment_und_0]"
id="edit-entry-23245-field-entry-attachment-und-0-upload"
style="background-color: transparent;">
I am able to locate the id using xpath or css however the numbers within the id string changes as this is randomly generated so the next time my test runs, it fails because it cant locate the string.
I would like to know if it is at all possible to write an xpath expression that will look for everything from the start of the string edit-entry- then some how look for any integer value between 0-9 within that string -23245-, then also match the end part field-entry-attachment-und-0-upload. this way when my test runs, it is able to locate the element all the time even if the numbers within the string change. iv tried adding \d+ to my xpath but it doesn't seem to pick it up.
This is the xpath:
//*[#id="edit-entry-23245-field-entry-attachment-und-0-upload"]
That is because your Xpath isn't extracting the right attribute. Use an Xpath like this to get the id of the element:
//input[#type="file" and #class="form-file"]/#id
This Regex should extract the value you are looking for:
/edit-entry-\d+-field-entry-attachment-und-0-upload/
If \d+ doesn't work for you this is another possibility:
/edit-entry-[0-9]+-field-entry-attachment-und-0-upload/
Just an idea for workaround, since we can't use regex in pure XPath.
In case you just need to match <input> element with id equals "edit-entry-[arbitrary-characters-here]-field-entry-attachment-und-0-upload", we can use starts-with() and ends-with() functions like this :
//*
[starts-with(#id, "edit-entry-")
and
ends-with(#id, "-field-entry-attachment-und-0-upload")]
and in case you're using XPath 1.0 where ends-with() function is not available :
//*
[starts-with(#id, "edit-entry-")
and
(
"-field-entry-attachment-und-0-upload"
=
substring(#id, string-length(#id) - string-length("-field-entry-attachment-und-0-upload") +1)
)
]

Selenium IDE - Select checkbox on table row

I'm using Selenium IDE and I have a table where it has many rowns and columns. Each row has its own checkbox to select this row.
I was using this command to search for a specific row:
css=tr:contains('US Tester4') input[type="checkbox"]
But the problem is that in this colum, I have some other similar words like "US Tester41", "US Tester42" ... and when I use this command, it selects the wrong row.
I thought if I replace this word "contains" for some other like "equals" or "exactly" would work, but it didn't (I don't know the sintax).
Any ideas?
Follow the screenshot:
http://oi41.tinypic.com/2ake9hw.jpg
I'm not familiar with Selenium IDE, but with the selenium webdriver I would use an xpath. So I guess something like this will work for you:
xpath=//tr[td[3][text()='US Tester4']]//input[#type='checkbox']
This worked for me:
//tr//td[.='US Tester4']//input[type="checkbox"]
against:
<table>
<tr><td>US Tester</td>input(type="checkbox")</tr>
<tr><td>US Tester4</td>input(type="checkbox")</tr>
<tr><td>US Tester41</td>input(type="checkbox")</tr>
<tr><td>US Tester412</td>input(type="checkbox")</tr>
</table>
It matched the second element.
This worked for me
xpath=(//input[#name='uid'])[2])
The 2 being the order of elemets
I'm not very familiar with the IDE but I have used the Webdriver before. If possible I would use this xpath.
xpath = "//td[.= 'US Tester4']//previous-sibling::td//input[#type = 'checkbox']"
This should locate only one element on screen. Using previous-sibling and following-sibling is very helpful when you haven't got a good enough identifier on the exact element you want to find. In your case the which contains the checkbox hasn't a good identifier where as the after has text which you could match using the '=' operator. You just need to use the 'previous-sibling' to find the with the checkbox

Create a regex for a string with 1 item that changes

I am trying to build a regex for an inline CSS code that 1 item on changes
This is the line of code in question
<div="Box1" style="background-color:Transparent;border-color:Transparent;border-style:None;height:436px;"></div>
I need to be able to pick this out but the height is different on every page
so all the rest is exactly the same but the height changes
If you got that line, you can use the following regex to get the height.:
'<div="Box1" style="background-color:Transparent;border-color:Transparent;border-style:None;height:436px;"></div>'
.match(/height:([\sa-z0-9]+);/)
This will return:
["height:436px;", "436px"]
This example is in JS, I don't know in what language you want to use the Regex? But in CSS you cant.
[0-9]+ matches an arbitrary number.
However, for the HTML part you should not use a regex at all but a HTML parser - and then only use a regex on the style attribute.

Resources