ruby cucumber click href in table - html-table

I am new to ruby as well as cucumber, trying to automate.
I need to click href link inside a table (Check1 and Check2). I tried:
#browser.link(:href => "http://googlenet.com.au").click
#browser.table(:href => "http://googlenet.com.au").click
as well as:
#browser.find(:linkText => "Check1").click() \\{capybara}
I am getting unable to locate element error or undefined method find while running.
How do I get it clicked? If I had to use capybara is there any config needed to be done in env.rb or any other file so as to include capybara gem?
My HTML LOOKS like:
<html webdriver="true">
<head></head>
<body class="background_colour" onload="loadHeader()">
<div id="content_wrapper">
<h2></h2>
<br></br>
<table class="menuTable">
<tbody>
<tr>
<td width="5%"></td>
<td width="25%">
<strong></strong>
</td>
<td width="70%"></td>
</tr>
</tbody>
</table>
<table class="menuTable">
<tbody>
<tr>
<td width="10%"></td>
<td width="25%">
<h3>
<!-- B:Registration Link[START] Mod… -->
<span class="black_font"></span>
Check1
<!-- B.Registration Link[START] Mod… -->
</h3>
</td>
<td width="65%"></td>
</tr>
<tr>
<td width="10%"></td>
<td width="25%">
<h3>
<span class="black_font"></span>
Check2
</h3>
</td>
<td width="65%"></td>
</tr>
</tbody>
</table>
<table class="menuTable">
<tbody>

Well you can use
click_link('Check1')
click_link('Check2')
Take a look at this aswell
https://www.launchacademy.com/codecabulary/learn-test-driven-development/rspec/capybara-cheat-sheet
Edit
With regards to your no method errors this is a basic setup i use that doesn't give me any problems
Gemfile
source 'https://rubygems.org'
gem 'cucumber'
gem "capybara"
gem 'rspec'
gem 'selenium-webdriver'
capybara_drivers.rb
Capybara.register_driver :firefox do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
profile.native_events = true
Capybara::Selenium::Driver.new(app, :browser => :firefox)
end
env.rb
require 'capybara/cucumber'
require 'selenium-webdriver'

Related

Selenium IDE selecting from the list which is powered by Google

I'm new to Selenium IDE and I need to run some test on one website. Everything seems to work just fine except the part where I need to select my location since that list is powered by Google I can't really get the object by list id..
Here's my test so far:
<tr>
<td>open</td>
<td>/</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>id=newregister-form-email</td>
<td>javascript{"test+" + Math.floor(Math.random()*11111) + "#gmail.com";}</td>
</tr>
<tr>
<td>type</td>
<td>id=newregister-form-pass</td>
<td>test</td>
</tr>
<tr>
<td>clickAndWait</td>
<td>css=input.submitLogin</td>
<td></td>
</tr>
<tr>
<td>waitForPageToLoad(1000)</td>
<td></td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>id=u_users-form-name</td>
<td>Tester</td>
</tr>
<tr>
<td>type</td>
<td>id=u_users-form-surname</td>
<td>Test</td>
</tr>
<tr>
<td>type</td>
<td>id=u_users-form-phone</td>
<td>javascript{"+3706" + Math.floor(Math.random()*11111);}</td>
</tr>
<tr>
<td>type</td>
<td>id=cityinput</td>
<td>Vilnius</td>
</tr>
<tr>
<td>pause</td>
<td>1000</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>id=cityinput</td>
<td>Vilnius</td>
</tr>
The last part is where I need to choose from the dropdown list but Selenium IDE doesn't see it when recording.
Thank you in advance
Update:
This is html code of the city input :
<div class="row field">
<div class="col-xs-12 col-sm-8 col-sm-offset-4 col-md-offset-3 formelement-wrapper">
<input type="text" id="u_users-form-country" name="u_users-form[country]" value="" />
</div>
</div>
<div class="row field">
<div class="col-xs-12 col-sm-8 col-sm-offset-4 col-md-offset-3 formelement-wrapper">
<input type="text" id="u_users-form-city" name="u_users-form[city]" value="" />
</div>
</div>
<div class="row field">
<div class="col-xs-12 col-sm-8 col-sm-offset-4 col-md-offset-3 formelement-wrapper">
<input type="text" id='cityinput' placeholder="Miestas, Šalis">
</div>
</div>
instead of a pause you should use a waitForElementPresent on id=cityinput. This will allow you to wait until that element is present and not just 1 second (as your pause is currently doing)
<tr>
<td>waitForElementPresent</td>
<td>id=cityinput</td>
<td></td>
</tr>

Unable to locate element using xpath

Am very new to Selenium just learnt few things and trying to automate.Unable to locate an element in Selenium webdriver for the below one:
<div class="navBg withSubMenu">
<table id="topnav" class="navTable" cellspacing="0" cellpadding="0" style="-moz-user-select: none; cursor: default;">
<tbody>
<tr>
<td class="logoCell navCell" valign="top">
<td class="navItem navCell relative selected">
<td class="navItem navCell relative notSelected">
<a class="content tasks" href="/tasks/otasklist.do">
<div class="label" style="z-index:155; ">TASKS</div>
<div class="img"> </div>
</a>
</td>
<td class="navItem navCell relative notSelected">
<a class="content reports" href="/reports/reports.do">
</td>
<td class="navItem navCell relative notSelected">
<td class="menuCell navCell" valign="top">
</tr>
<tr class="secondLevelRow">
</tbody>
</table>
I have written code like
driver1.findElement(By.xpath("//*[Contains(#class,'content tasks')]")).click();
Anyone please help me.
And also please suggest some sites or links to learn more about locators especially xpath. I tried few but not getting it in depth.
Thanks in advance.
Start contains with a lower case c:
driver1.findElement(By.xpath("//*[contains(#class, 'content tasks')]")).click();
<a class="content tasks" href="/tasks/otasklist.do">
driver1.findElement(By.xpath("//a[#class='content tasks']")).click();

How to select radio button in selenium web driver java using text of nearby element in XPath

I need to select radion button with id="optProfile" in 7th tr using text "Internet Users" in td/font tag. I am using below code but it is not working and selecting radio button from 1st tr row. Can anyone help
<html>
<head>
<body onmousedown="infCheckMouseDown(event);" onkeydown="infCheckDownKey(event);" onbeforeunload="INFIEExit();" onload="INFsetInitialFocus();window_onload();CreateScriptingFrames();">
<form action="PEINFCommon.ASP?WCI=Genericcreen&WCE=GenericEvent" onsubmit="return false" name="Genericcreen" method="post">
<table class="TopLevelTable" align="center" width="100%" border="0">
<tbody>
<tr>
<td width="100%">
<table width="100%" border="0">
<tbody>
<tr>
<tr>
<tr>
<tr>
<tr>
<td width="100%">
<div align="center">
<center>
<table width="98%" cellspacing="0" cellpadding="0" border="0" cols="1">
<tbody>
<tr align="center">
<td align="left">
<table width="100%">
<tbody>
<tr>
<tr>
<tr>
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<td width="5%">
<input id="optProfile" type="Radio" callfunction="" value="" onblur="ResetScript(this);return true;" onfocus="HighlightScript(this);" onclick="" name="" size="11">
<font class="fLabel"></font>
<br>
<input id="hidCurrentProfile" type="hidden" onblur="ResetScript(this);" onfocus="HighlightScript(this);" value="N" callfunction="" size="11" name="/Root/ACORD/InsuranceSvcRs/nk_ck_com.cc_FetchUserProfileRs/nk_ck_com.cc_SecurityDetails/nk_ck_com.cc_AvailableProfile[12]/nk_ck_com.cc_CurrentProfileInd">
<input id="hidProfile" type="hidden" onblur="ResetScript(this);" onfocus="HighlightScript(this);" value="RACINTER" callfunction="" size="11" name="/Root/ACORD/InsuranceSvcRs/nk_ck_com.cc_FetchUserProfileRs/nk_ck_com.cc_SecurityDetails/nk_ck_com.cc_AvailableProfile[12]/nk_ck_com.cc_ProfileCd">
</td>
<td>
<font class="">Internet Users</font>
</td>
</tr>
<tr height="">
<tr height="">
<tr height="">
<tr>
<tr>
<tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</center>
</div>
</td>
</tr>
<tr>
<tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
</frame>
<frame id="Script" scrolling="Yes" frameborder="0" noresize="">
</frameset>
</html>
you need to use ancestor instead of preceding:
//font[text()='Internet Users']/ancestor::tr[1]/td/input[#type='Radio']
and it's also a better practice to tell which one of the tr-ancestors you want -> [1] - since you want the first direct ancestor
Is there a reason why you are not using the id of the radio button?
WebElement wElement = wDriver.findElement(By.Id("optProfile"));
You can use the following XPath
//tr[.//font[text()='Internet Users']]//input[#id='optProfile']
This xpath looks for the tr containing the font tag with text "Internet Users" and then navigates to the input tag with id = 'optProfile'
You can use preceding as below-
By.xpath("//font[contains(text(),'Internet Users')]/preceding::input[#id='optProfile']")
Thanks everyone here for your help. I really appreciate it.Its working with below code
WebElement trs = GlobalVar.wDriver.findElement(By.xpath("//font[text()='Internet Users']/ancestor::tr[1]"));
trs.findElement(By.id("optProfile")).click();
I would try something like this
List<WebElement> trs = driver.findElements(By.cssSelector("table > tr"));
for (WebElement tr : trs)
{
List<WebElement> fonts = tr.findElements(By.tagName("font"));
if (!fonts.isEmpty() && fonts.get(0).getText().trim().equals("Internet Users"))
{
tr.findElement(By.id("optProfile")).click();
break;
}
}
You didn't show the TABLE tag in the HTML snippet so you will likely need to be more specific in the initial CSS selector. Basically this code loops through the TRs looking for "Internet Users" in the FONT tag. Once it finds it, it click the radio button with id=optProfile.
If you will post the TABLE tag in the HTML snippet I can help with the CSS selector.
Edit 1: changed the code to address the case where some TRs don't contain FONT tags.

Selenium mouseOver images at child nodes using DynaTree

Thanks for taking the time to ready my questions. I need to mouseover then mousedown on an image (cog.png) for a specific node child represented by text to the right of an image. I can successfully Target the class but cannot work out how to target the image alone.
HTML, I want to target the cog.png near 'Australia'
<div id="tree" class="">
<ul class="dynatree-container dynatree-no-connector">
<li class="dynatree-lastsib">
<span class="dynatree-node dynatree-expanded dynatree-has-children dynatree-lastsib dynatree-exp-el dynatree-ico-e"></span>
<ul style="">
<li class="">
<span class="dynatree-node dynatree-exp-c dynatree-ico-c" style="background-color: transparent;">
<span class="dynatree-connector"></span>
<img alt="" src="/icn/cog.png"></img>
<a class="dynatree-title" title="This option is Active" href="#">
Australia
</a>
</span>
Target code for Selenium IDE (which targets the full span, I just want the image cog.png)
//span[contains(#class,'dynatree-node') and //img[#src='/icn/cog.png'] and .//text()='Australia']
I tried
//img[#src='/icn/cog.png']
Not surprisingly it was not specific enough and targeted the first instance of the image
Versions
Firefox 32.0.3
Selenium IDE: 2.7.0
This is the code that I used to navigate on the dynatree, it wsa made more difficult as the first item was added as a child, the remainder added below or above the child. I kept track by creating 'counts' to determine at what point the items were added.
<tr>
<td>waitForTextPresent</td>
<td>Australia</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>//span/a[contains(., '${LocationLevel1Ext}')]/preceding::span[#class='dynatree-expander'] [1]</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>//span/a[contains(., '${LocationLevel2Ext}')]/preceding::span[#class='dynatree-expander'] [1]</td>
<td></td>
</tr>
<tr>
<td>clickAt</td>
<td>//span/a[contains(., '${PreviousLocationName}')]/preceding::img[#src='/icn/cog.png'] [1]</td>
<td></td>
</tr>
<tr>
<td>waitForTextPresent</td>
<td>Add before this node</td>
<td></td>
</tr>
<tr>
<td>mouseOver</td>
<td>//span[contains(text(), 'Add before this node')]</td>
<td></td>
</tr>
<tr>
<td>mouseDown</td>
<td>//span[contains(text(), 'Add before this node')]</td>
<td></td>
</tr>

Any Dijit component is rendered as TextField

Please find my code which intents to develop a Form. Help me in understanding why the CheckBox and NumberSpinner are rendered as TextFields.
I have imported all the required classes and dojo config attribute is configured properly
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%# taglib prefix="fmt" uri=`enter code here`"http://java.sun.com/jstl/fmt" %>
<%#include file="/webclient/admin/jspf/NotificationOperations.jspf" %>
<!-- load dojo and provide config via data attribute -->
<script type="text/javascript" src="/webclient/map/js/dojo/dojo.js" data-dojo-config="parseOnLoad: true"></script>
<script>
require([
"dojo/parser",
"dijit/form/CheckBox",
"dijit/form/NumberSpinner",
"dojo/domReady!"
], function(parser){
parser.parse();
});
</script>
<%#include file="/webclient/common/jspf/jsUtility.jspf" %>
<html>
<head>
<body class="claro">
<style>
#import "/webclient/common/js/dojo/dgrid/css/skins/claro.css";
h2 {
margin: 12px;
}
.heading {
font-weight: bold;
padding-bottom: 0.25em;
}
.ui-widget{
margin: 10px;
}
</style>
<table width="100%" height="100%">
<tr><td>
<div id="DomainMapContainer" >
<tr class="spacer"><td> </td></tr>
<tr><td>
<form id="addProfile" data-dojo-type="dijit/form/Form" data-dojo-props='encType:"multipart/form-data", action:"/prov/createRFC2544TestProfile.do?action=createRFC2544TestProfile", method:"POST", target:"formSubmitIframe"'>
<table>
<tr>
<td><label for="profileName">Profile Name</label></td>
<td>
<input data-dojo-type="dijit/form/TextBox" data-dojo-props='id:"profileName", name:"profileName", required:"true"'/>
</td>
</tr>
<tr>
<td><label for="comments">Comments</label></td>
<td>
<input data-dojo-type="dijit/form/TextArea" data-dojo-props='id:"comments", name:"comments", rows:"5", cols:"40"'/>
</td>
</tr>
<tr>
<td><label for="frameSize">Frame Size</label></td>
<td>
<table>
<tr>
<td><label for="cb_1"><input data-dojo-type="dijit/form/CheckBox" id="cb_1" name="cb_1" value="64" checked/> 64</label></td>
<td><label for="cb_2"><input data-dojo-type="dijit/form/CheckBox" id="cb_2" name="cb_2" value="128"/> 128</label></td>
</table>
</td>
</tr>
</table>
<fieldset>
<legend><label> Throughput Test</label></legend>
<table>
<tr>
<td><label for="tTestDuration">Test Duration</label></td>
<td><input data-dojo-type="dijit/form/NumberSpinner" id="tTestDuration" value="60" data-dojo-props="smallDelta:1, constraints:{min:1,max:600,places:0}" name="tTestDuration"/>(Seconds)</td>
</tr>
<tr>
<td><label for="tTestMinFrameRate">Minimum Frame Rate</label></td>
<td><input data-dojo-type="dijit/form/NumberSpinner" id="tTestMinFrameRate" value="0" data-dojo-props="smallDelta:1, constraints:{min:1,max:10000,places:0}" name="tTestMinFrameRate"/>(Mbps)</td>
</tr>
<tr>
<td><label for="tTestMaxFrameRate">Maximum Frame Rate</label></td>
<td><input data-dojo-type="dijit/form/NumberSpinner" id="tTestMaxFrameRate" value="100" data-dojo-props="smallDelta:1, constraints:{min:1,max:10000,places:0}" name="tTestMaxFrameRate"/>(Mbps)</td>
</tr>
<tr>
<td><label for="tTestStepSize">Step Size</label></td>
<td><input data-dojo-type="dijit/form/NumberSpinner" id="tTestStepSize" value="5" data-dojo-props="smallDelta:1, constraints:{min:1,max:100,places:0}" name="tTestStepSize"/>(Mbps)</td>
</tr>
<tr>
<td><label for="tTestAcceptableLoss">Step Size</label></td>
<td><input data-dojo-type="dijit/form/NumberSpinner" id="tTestAcceptableLoss" value="0" data-dojo-props="smallDelta:0.1, constraints:{min:0,max:10,places:0}" name="tTestAcceptableLoss"/>(%)</td>
</tr>
</table>
</fieldset>
</form>
</td></tr>
</div>
</td></tr>
</table>
</body>
</head>
</html>
I'm seeing a few things here, first of all you're missing a closing </tr> tag when you close the "frame size" table. I suppose that it won't cause errors, but you can never be too sure.
You're also closing your </head> tag at the bottom of the page. You should close it before the <body> starts.
Then the real problem is that your widgets are not working because there are errors while parsing your HTML. The cause of the error is the "comments" text area. The correct module name you should use is dijit/form/Textarea, so the "a" of area should be in lowercase.
Besides that, you should also add the dijit/form/Textarea module to your list of modules (in your require() block).
If you did all that it should work, as you can see in this fiddle: http://jsfiddle.net/9U5AB/