Using OR with Selenium.Click - is this possible? - selenium

There is an image and a link on this webpage. Clicking on either of them does the same job. I was wondering if there is an option of including OR in Selenium.Click. Something like:
Selenium.Click("image") OR Selenium.Click("link");

Selenium doesn't offer OR condition. If the requirement is to click on either of the locators depending on which one is available, then you can easily create a custom method
This is written in JAVA, you can change it to your programming language
public void ClickOnAvailableLocator(String locator1, String locator2)
{
if(selenium.isVisible(locator1)
selenium.Click(locator1);
else
selenium.Click(locator2);
}

No, if you need to test both routes I would suggest two tests.

Related

Create a Reusable method to Verify list elements by Selenium WebDriver

I am new to this site and don't know how things show up here. I was reading the post from below where String array is being used to ListwebElements.
Verify list elements by Selenium WebDriver
String[] expected = {"GRAM", "OUNCE", "POUND", "MILLIMETER", "TSP", "TBSP", "FLUID_OUNCE"};
I am trying to do something similar using String Array trying to get different buttons on a UI page. I want to make this method reusable by changing the "expected" list per test. Does anyone know how you would make this method Reusable?
In my case, "expected" list is different each time depending on a page.
I would suggest to use the custom annotation for reusing purpose. Please see the example here.
If you are using JUnit you can also use parameterized test

Is it better to use Actions.sendKeys() or WebElement.sendKeys() to simulate a user typing with Selenium WebDriver?

As far as I'm aware there are two ways of typing with Selenium:
new Actions(webDriver).sendKeys("text to send").perform();
webElement.sendKeys("text to send");
The Actions method seems like the most natural way of replicating a user typing as the keys are sent wherever the browser wants (I believe a method called sendKeysToActiveElement is being used under the covers). However, many tutorials instruct testers to use the WebElement method (this is in fact the only option when using SafariDriver), I assume as it is simpler.
Is the Actions method actually a better simulation of user interaction, or should I use the WebElement method for convenience?
public Actions sendKeys(java.lang.CharSequence... keys)
Sends keys to the active element. This differs from calling WebElement.sendKeys(CharSequence...) on the active element in two ways:
The modifier keys included in this call are not released.
There is no attempt to re-focus the element - so sendKeys(Keys.TAB) for switching elements should work.
for more you can refer this link : https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/interactions/Actions.html#sendKeys-java.lang.CharSequence...-
This doesn't answert the question directly but I would have thought using SendKeys in the WebElement class would have been better, since you already have the WebElement object in memory, why do you need to create an Actions object?
I have always used the WebElement.SendKeys() method and I have not found any uses to switch over to using the Actions class for sending over a regular string.
I would use the Actions class when I require more complex scenarios e.g. Need to hold down a button or drag something.
Two things:
If you want to recreate exactly the interactions of a user, it is better to use Actions, otherwise it is better to use webElement.sendkeys() because it is more practical and simple.
If you need to execute a complex action like drag and drop it is also better to use Actions.

How do you select elements in selenium that have a period in their id?

I am trying to write code that will select all options for an HTML drop-down menu. I have written the following code which I believe should work.
public void testSelectMultipleOptions () {
// code to get to report page
selectAllOptions("param.Status");
// code to run report and switch to the result page
}
public void selectAllOptions(String htmlID) {
List<WebElement> options = selenium.findElements(By.cssSelector("select#"+htmlID+" > option"));
for(WebElement option: options) {
option.click();
}
}
When I run this code no options are selected in the drop-down. I believe the problem I am having is caused by the fact that I have an HTML element with a period in the id but I do not have the ability to change the underling HTML code for the page.
There is incredibly bad development practice. I know you don't have the ability to change it, but if you can, point out that it's very very bad. Why? Two reasons.
In CSS, the rules based on classes generally start with a period.
In CSS selector frameworks, including jQuery/Sizzle and what Selenium is doing in this example, the period has a special meaning - mainly to select elements based on many rules. This is why it's tripping here here - you can see the same thing if you run the CSS selector direct into Chrome or Firebug - it will fall over as well.
Using a period in the ID for your elements is going against all this. Annoyingly the HTML spec allows for this.
Anyway, all is not lost, there are many ways around it.
First, you can escape it:
select#param\\.Status
Second, you could use a slightly more elaborate selector:
select[id='param.Status']
Finally, you could use XPath:
//select[#id='param.Status']

FindRow with text values inside span attributes

I am using Watin to find rows in a jQuery table. However, jQuery sets some of its text in a cell within a span class. Watin does not work in these cases.
WebBrowser.Current.Table("grid").FindRow("Liza", columnNum)
The above code works for:
<td>Liza</td>
but not for:
<td><span>Liza</span></td>
Any clue how I can tweak the Watin code to work with span classes?
You might want to try using the Text property to identify
WebBrowser.Current.Table("grid").FindRowInOwnTableRows(t => t.Text.Trim()== rowidentifier, columnNum);
I had some similar issues, my recommendation is to create a helper class that's available to all your specs and you add helper functions there, such as removeHtml, wait for async request, this kind of thing. a good starting point would be these methods http://www.dotnetperls.com/remove-html-tags
So on your tests you can do something like
// where .NoHTML() is a extension method in your helper class
WebBrowser.Current.Table("grid").FindRow(value.NoHTML(), columnNum)
This might not be the exact answer, but I hope it helps

Generic selenium testing images

With selenium can one run a test which check all images on a current page if they contain ALT attribute and report images not having it ?
Yes. The first thing you need to do is decide which selenium you want to use.
You can use Selenium IDE, where a test consists of an HTML table.
You can use Selenium RC, where you write a test in a programming language (eg. C#, Java, PHP, Ruby, etc).
The latter is a little more complex at first, but if you want real power, you will use that method.
Then, you'll need to learn how to find all the images on a page.
//img is a good XPath query to use. For Xpath details, see w3schools, and especially this page.
Then you'll want to find images with an alt attribute: //img[#alt]
One approach would be to count how many images there are, and subtract the number with alt attributes.
You can also do this in WebDriver (soon to be Selenium 2). The following example is for TestNG/Java but other client languages are available.
List<WebElement> images = driver.findElements(By.xpath("//img[not(#alt)]"));
assertEquals(images.size(), 0);
For more feedback you could also use something like the following to output details of the images without alt attributes:
for(WebElement image : images) {
System.out.println(image.getAttribute("src"));
}
We have a similar test but we grab the page's Html and parse out the images with a regular expression and then match on a second regular expression looking for the alt tag. I haven't done a speed test, but I think it might be faster than doing the Xpath route.