Multiple Selectors in Selenium Test - css

I want to test a page with responsive layout.
Some elements (like the menu) will be different depending on resolution.
how can I target them to run with the same test classes?
i'm thinking about How.CSS, using = "a.normal-link, a#responsive-link"
is this a good idea? is there a better way?
and is it possible to "inject" some kind of workaround-logic for specific occasions? (for example: if the normal login-button is not visible, try to open the responsive menu, then click the other (responsive) login-link)
I was just thinking about a try-catch block, but that feels wrong for an alternative route.
thanks

As, you need to check whether an element is visible or not, it is better to use if/else blocks, to validate the same. Sample piece of code is given below:
if(driver.findElement(<Your element>).isDisplayed()) {
// Perform your operations
} else {
// Open the button from menu item
}
Hope this helps.

Related

Detect blocking overlay with selenium

I'm testing a website that opens in-browser pop-ups to display object details. These pop-ups are sometimes modal, by which I mean that they render the rest of the screen inoperative and trigger a gray transparent overlay that covers everything but the pop-up. This overlay is intended behavior, which means that I need a way to detect whether or not it was correctly triggered.
However, I am not familiar enough with the implementation of such overlays to determine where in the DOM I should look to find the properties that govern such behavior. As such, I was hoping someone with more information on how such overlays are usually configured could point me in the right direction.
The obvious solution is to simply try to click a button and see what happens but I was hoping to write a method that I could implement throughout the test suite rather than having to write a different check for each circumstance.
For those interested I'm scripting in Java using Selenium.
I know this is old, but it may still help someone else. I had just recently solved a similar problem for our React site. I believe we were using the react-block-ui module to implement our blocking overlays.
Basically, I was able to detect a certain element was blocked by an overlay because of 2 known facts:
The element was within a containing div ("the overlay") that followed a certain naming convention. In our case, it was section-overlay-X.
This overlay would have a class attribute (named av-block-ui) if it was blocking.
(Hopefully, you have access to this information, too... or something similarly useful.)
With this information, I wrote up a couple utility methods to help me determine whether or not that particular WebElement is blocked by an overlay. If it was blocked, throw a ElementNotInteractableException.
For Java:
...
By SECTION_OVERLAY_ANCESTOR_LOCATOR = By.xpath("./ancestor::div[contains(#id, 'section-overlay-')][1]");
...
private WebElement findUnblockedElement(By by) {
WebElement element = driver.findElement(by);
if (isBlockedByOverlay(element)) {
throw new ElementNotInteractableException(String.format("Element [%s] is blocked by overlay", element.getAttribute("id")));
} else {
return element;
}
}
private boolean isBlockedByOverlay(WebElement element) {
List<WebElement> ancestors = element.findElements(SECTION_OVERLAY_ANCESTOR_LOCATOR);
WebElement overlayAncestor = ancestors.get(0);
String overlayClass = overlayAncestor.getAttribute("class");
return !StringUtils.isBlank(overlayClass);
}
Here's my snippet on it:
https://bitbucket.org/snippets/v_dev/BAd9dq/findunblockedelement
This won't work in all situations, but I solved this problem by checking the overflow value of the body element. The flavor of modal I was trying to get past disabled scrolling of the page while it was active.

How to render checked checkboxes using CSS alone?

This is may be very noobish and a bit embarrassing but I am struggling to figure out how to make checkboxes 'checked' using CSS?
The case is that if a parent has a class setup (for example) I'd like to have all the checkboxes having setup as parent to be checked. I'm guessing this is not doable in pure CSS, correct? I don't mind using JS but am just very curious if I could toggle the state of the checkboxes along with that of their parent (by toggling the class).
Here's a fiddle to play around with.
A checkbox being "checked" is not a style. It's a state. CSS cannot control states. You can fake something by using background images of check marks and lists and what not, but that's not really what you're talking about.
The only way to change the state of a checkbox is serverside in the HTML or with Javascript.
EDIT
Here's a fiddle of that pseduo code. The things is, it's rather pointless.
It means you need to adding a CSS class to an element on the server that you want to jQuery to "check". If you're doing that, you might as well add the actually element attribute while you're at it.
http://jsfiddle.net/HnEgT/
So, it makes me wonder if I'm just miss-understanding what you're talking about. I'm starting to think that there's a client side script changing states and you're looking to monitor for that?
EDIT 2
Upon some reflection of the comments and some quick digging, if you want a JavaScript solution to checking a checkbox if there's some other JavaScript plugin that might change the an attribute value (something that doesn't have an event trigger), the only solution would be to do a simple "timeout" loop that continuously checks a group of elements for a given class and updates them.
All you'd have to do then is set how often you want this timeout to fire. In a sense, it's a form of "long polling" but without actually going out to the server for data updates. It's all client side. Which, I suppose, is what "timeout" is called. =P
Here's a tutorial I found on the subject:
http://darcyclarke.me/development/detect-attribute-changes-with-jquery/
I'll see if I can whip up a jQuery sample.
UPDATE
Here's a jsfiddle of a timeout listener to check for CSS classes being added to a checkbox and setting their state to "checked".
http://jsfiddle.net/HnEgT/5/
I added a second function to randomly add a "checked" class to a checkbox ever couple of seconds.
I hope that helps!
Not possible in pure css.
However, you could have a jQuery event which is attached to all elements of a class, thereby triggering the check or uncheck based on class assignments.
Perhaps like this:
function toggleCheck(className){
$("."+className).each( function() {
$(this).toggleClass("checkedOn");
});
$(".checkedOn").each( function() {
$(this).checked = "checked";
});
}

How to find all dialogs in JQuery

I am trying to bind an event to all dialogs that have been created on a page using the JQuery UI Dialog function (whether they have been displayed or not). I can't seem to figure out a selector that will get me there. I've tried both .ui-dialog and .ui-dialog-content without success.
Since I'm trying to make a generic method, I won't know the IDs of the dialogs that may have been created.
I'm using the following code to test. It works if I specify a dialog's id (#mydialog), but in production, I won't know these.
$("div.ui-dialog").bind("dialogclose", function(event, ui) {
window.alert("close fired");
}
Do your dialogs have a common class that you can select them with? If they all have the "ui-dialog" class then this will work:
$(".ui-dialog")
Your example of
$("div.ui-dialog")
Is asking to select all divs with a class of ui-dialog, which should probably also work as long as the class is given to a div element.
Your problem might be that you binding the dialog elements before they exist? You might want the .live() function instead so that it binds to any dialogs created at any point and not just the ones that exist when the function is called.
Posting an HTML snippet would help.
You can use this:
$(":ui-dialog").each(function(){
"enter your code here"
})

How to turn on line number tooltip while scrolling infragistics ultrawebgrid

I once saw this feature in action but I don't know how to turn it on. The grid can show a tooltip with the current row number (or row ID) while dragging the scrollbar. This helps you to stop the scroll in the right place. I'm assuming some property will turn this on, but I can't find it.
Maybe it is also dependent on the scroll mode?
UPDATE:
In the image below you can see an example of the tooltip I'm looking for. This is displayed while the scrollbar is being dragged (up or down). The number in the tooltip is the row number (you can't see it in this image, way to the left in the grid). This is the same grid that I am using now. Just from a very old build of our product. Somehow this tooltip was turned off. And no one knows how to turn it back on :(
I'm pretty sure this is a built in feature of the ultrawebgrid. Not something that required extra coding.
alt text http://img138.imageshack.us/img138/6337/croppercapture.jpg
Right! Now that we've established that you have version 6.3, I've hopefully got a solution for you. I don't have 6.3 myself, but I've got a slightly later one that I think didn't have Virtual Scrolling added as a feature yet.
So try this code:
webgrid.DisplayLayout.XmlLoadOnDemandType = XmlLoadOnDemandType.Virtual;
This should automatically put a tooltip on the grid as you scroll down. Have a look here for a running sample... (and remember to choose the virtual option)
Here's hoping!
Rob G
I don't know if there's a UltraWebGrid property to simply turn on the behavior you're looking for. I almost suspect you experienced this feature in another application, perhaps not even a web based one (sorry!). I do however, know exactly what you're talking about.
As a work-around, I would suggest allowing the user to input the destination row number, and to simply "jump" to it, using this technique.
If that doesn't satisfy you, it may be possible to achieve this behavior with JavaScript. You would need to use something like this technique to get the information you need, estimate (or actually detect, if possible) the row number, and the rest is up to the GUI. I would go with the work-around described above though :)
I'm typing this from memory here as I don't have it installed on this machine and I haven't seen that setting before, but how about adding something like this to the InitializeRow event:
foreach (UltraGridCell cell in e.Row.Cells)
{
if(cell.Column.Key == "Topic") //from your grid above
cell.Title = cell.Row.Index;
}
The row object itself does not have a "Title" property from memory, but the cell does.
See if that works...
Regards,
Rob G
OK - I think I've found your illusive setting:
You can set the TipStyleScroll on the Override to Show on the Grid (this may be version dependant).
You can determine which field is displayed as the tooltip by using the ScrollTipField property of the band.
I did it like so:
myGrid.DisplayLayout.Override.TipStyleScroll = TipStyle.Show;
myTopBand.ScrollTipField = "Id";
...and it works like a charm!
If it's a really long list, sometimes setting the ScrollStyle to Deferred helps:
myGrid.DisplayLayout.ScrollStyle = ScrollStyle.Deferred;
Hope that helps...
Rob G
Once again - not sure which version you have, so to be safe here's somthing you can try from 2009 version:
myGrid.Behaviors.VirtualScrolling.Enabled = true;
myGrid.Behaviors.VirtualScrolling.TooltipVisibility = DefaultableBoolean.True;
If your scrolling mode is Deferred instead of Virtual, then the tooltip is normally enabled by default.
You can find full details about this feature here
Hope that helps,
Rob G

How to prevent a hyperlink from linking

Is it possible to prevent an asp.net Hyperlink control from linking, i.e. so that it appears as a label, without actually having to replace the control with a label? Maybe using CSS or setting an attribute?
I know that marking it as disabled works but then it gets displayed differently (greyed out).
To clarify my point, I have a list of user names at the top of my page which are built dynamically using a user control. Most of the time these names are linkable to an email page. However if the user has been disabled the name is displayed in grey but currently still links to the email page. I want these disabled users to not link.
I know that really I should be replacing them with a label but this does not seem quite as elegant as just removing the linking ability usings CSS say (if thats possible). They are already displayed in a different colour so its obvious that they are disabled users. I just need to switch off the link.
This sounds like a job for JQuery. Just give a specific class name to all of the HyperLink controls that you want the URLs removed and then apply the following JQuery snippet to the bottom of your page:
$(document).ready(function() {
$('a.NoLink').removeAttr('href')
});
All of the HyperLink controls with the class name "NoLink" will automatically have all of their URLs removed and the link will appear to be nothing more than text.
A single line of JQuery can solve your problem.
I'm curious on what it is you which to accomplish with that. Why use a link at all?
Is it just for the formatting? In that case, just use a <span> in HTML and use stylesheets to make the format match the links.
Or you use the link and attach an onClick-Event where you "return false;" which will make the browser not do the navigation - if JS is enabled.
But: Isn't that terribly confusing for your users? Why create something that looks like a link but does nothing?
Can you provide more details? I have this feeling that you are trying to solve a bigger problem which has a way better solution than to cripple a link :-)
A Hyperlink control will render as a "a" "/a" tag no matter what settings you do. You can customize a CSS class to make the link look like a normal label.
Alternatively you can build a custom control that inherits from System.Web.UI.WebControls.HyperLink, and override the Render method
protected override void Render(HtmlTextWriter writer)
{
if (Enabled)
base.Render(writer);
else
{
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.Write(Text);
writer.RenderEndTag(HtmlTextWriterTag.Span);
}
}
}
Could be a bit overkill, but it will work for your requirements.
Plus I find is usefull to have a base asp:CustomHyperlink asp:CustomButton classes in my project files. Makes it easier to define custom behaviour throughout the project.
If you merely want to modify the appearance of the link so as not to look like a link, you can set the CSS for your "a" tags to not have underlines:
a: link, visited, hover, active {
text-decoration: none;
}
Though I would advise against including "hover" here because there will be no other way to know that it's a link.
Anyway I agree with #pilif here, this looks like a usability disaster waiting to happen.
If you mean to stop the link from activating, the usual way is to link to "javascript:void(0);", i.e.:
foo
This should work:
onclick="return false;"
if not, you could change href to "#" also. Making it appear as a rest of text is css, e.g. displaying arrow instead of hand is:
a.dummy {
cursor:default;
}
Thanks for all the input, it looks like the short answer is 'No you can't (well not nicely anyway)', so I'll have to do it the hard way and add the conditional code.
If you are using databind in asp.net handle the databinding event and just don't set the NavigateUrl if that users is disabled.
Have you tried just not setting the NavigateUrl property? If this isn't set, it may just render as a span.
.fusion-link-wrapper { pointer-events: none; }
Another solution is apply this class on your hyperlink.
.avoid-clicks {
pointer-events: none;
}
CSS solution to make tags with no href (which is what asp:HyperLink will produce if NavigateURL is bound to null/empty string) visually indistinguishable from the surrounding text:
a:not([href]), a:not([href]):hover, a:not([href]):active, a:not([href]):visited {
text-decoration: inherit !important;
color: inherit !important;
cursor: inherit !important;
}
Unfortunately, this won't tell screen readers not to read it out as a link - though without an href, it's not clickable, so I'm hoping it already won't be identified as such. I haven't had the chance to test it though.
(If you also want to do the same to links with href="", as well as those missing an href, you would need to add pointer-events:none as well, since otherwise an empty href will reload the page. This definitely leaves screen readers still treating it as a link, though.)
In the OP's use case, if you still have the href being populated from the database but have a boolean value that indicates whether the link should be a 'real' link or not, you should use that to disable the link, and add a:disabled to the selector list above. Then disabled links will also look like plain text rather than a greyed-out link. (Disabling the link will also provide that information to screen readers, so that's better than just using pointer-events: none and a class.)
A note of caution - if you add these sorts of rules globally rather than for a specific page, remember to watch out for cases where an tag has no (valid) href, but you are providing a click handler - you still need those to look/act like links.

Resources