How to write locator for this highlighted input field element when ID and Class attribute are dynamic?
Also for this button
The "Course name" text is a notable feature of that block of HTML. Select it and use traversal commands to get to the <input>
cy.contains('span', 'Course name')
.next()
.find('input')
Alternatively, the placeholder text could be used (but it seems a bit generic)
cy.get('input[placeholder="Enter"]')
For the <button> a similar tactic
cy.contains('p', 'Add new course')
.prev('button')
.click()
Related
enter image description here
enter image description here
1)in the second picture Even fn_del is not recognized.
2)I want to use jquery function in the <button id="fileDel" th:onclick= part, please help
You are iterating input parameters with same id so you need to class or other attribute to identify FILE_NO, FILE_NAME. Also you can use just button's onclick action.
<input onclick="fn_del()"/>
there will be more than one button and input parameters because you define in a foreach loop.
Make sure that files variable have FILE_NO as well.
Does anyone know how to set a value to span tag using capybara?
I tried using element.set or element.send_keys, they only selected the targeted element without modifing the previous value.
<div data-offset-key="bbpvo-0-0" class="_1mf _1mj"><span data-offset-key="bbpvo-0-0"><span data-text="true">aa</span></span></div>
HTML snippet is above, I want to set aa to bb.
Capybara is designed to emulate a user - A user can't edit a span unless there's some sort of javascript widget attached to it. If you have a JS widget attached to the span you would need to perform whatever actions a user would do in order to edit the span. So you say the user has to click on the span and then type on the span - if that is so then you could try something like
span = find('span[data-text="true"]')
span.click
span.send_keys("new content", :enter) # if enter is needed to end the editing
which may work - although I'm going to guess the element actually gets replaced with an input or something after it's clicked on, in which case you need to figure out what those elements are (using the browsers inspector) and then find and use send_keys or set on that element instead
To set text in span value,jquery can be used with capybara as shown below:
page.execute_script("$("<span css selector>").text("testing")");
or
page.execute_script("$("<span css selector>").html("testing <b>1 2 3</b>")");
I am new to automation (Cucumber), and has very less idea of coding. I am looking for the script through which I can click on checkbox or radiobutton. Below is the HTML code I am looking at:
<"input class="facetoption" type="checkbox" value="facets.price_range%5B%5D=Rs.+2000+and+Below" autocomplete="off">
And below is the step definition which I tried
Step definition:
Then(/^Select the first Price Range Option$/) do
#browser.checkbox(:value => 'facets.price_range5B%5D=Rs.+2000+and+Below').click
end
The value in your locator doesn't match the value in the <input> tag. Compare the strings, and you'll see they are different.
your HTML: "facets.price_range%5B%5D=Rs.+2000+and+Below"
your code: "facets.price_range5B%5D=Rs.+2000+and+Below"
Update your step definition to the following, and it should work:
Then(/^Select the first Price Range Option$/) do
#browser.checkbox(:value =>'facets.price_range%5B%5D=Rs.+2000+and+Below').click
end
I don't know why you try to find checkbox by value. Better option is to find checkbox by id or class:
find('input#id').click()
If you still like to find checkbox by value please use:
find(:xpath, "input[#value='John']").click()
Use a regex so you can focus on the imporant parts, and ignore the unimporant parts
#browser.checkbox(:value => /2000\+and\+Below/).click
Using watir-webdriver, I am trying to set the value for a text field.
browser.text_field(:id, "phoneNumbers_value_input").set("5555551234")
When I run that command, I can see that watir found the field because the cursor is set on that field however no text is entered. I have also tried the send_keys and append commands but nothing seemed to work. No exception is thrown from these methods.
The only difference I could find between this field and the other fields on the page(which all work fine) is that it has this JQuery mask on it.
$(selector).mask("(999) 999-9999");
Any ideas on how to set the text field?
Edit:
Some more of the Javascript:
selector = '#' + id(field.id) + '_input';
if (field.format == 'phone') {
$(selector).mask("(999) 999-9999");
}
HTML of the field:
<div id="phoneNumbers_value_form_item" class="form_item validated no_focus">
<label for="phoneNumbers_value" class="form_label">phone</label>
<input type="text" value="" name="phoneNumbers[][value]" id="phoneNumbers_value_input" class="text">
<div class="tip"> </div>
<div class="tip_validating">
</div>
<div class="tip_error">
</div>
</div>
I don't see any element in the HTML you provided that would match the text input field being addressed in the ruby code at the top of your posting. e.g. there is nothing there with an ID of 'phone'
Based on the HTML in your question, I would expect the following to work
browser.text_field(:id, "phoneNumbers_value_input").set("5555551234")
Looking at the sample page you linked in the comments, when I use google chrome and the "Inspect Element" function on the input field with the ID of 'phone' I see that there are a number of event listeners associated with the field (blur, focus, input, keydown, keypress, unmask)
The 'focus' one gets my attention in particular, and seeing it there on that field makes me think that you might then need to first fire an event against the same element, such as the onfocus event, in order to activate the field, then try to set the value.
You'll notice that when you manipulate things manually, the field starts out blank, but as soon as it gets focus it displays the format for the input mask to the user, it may well be that this needs to happen first, before it see's any kind of input.
EDIT: In this case, based on feedback from the questioner, the answer turned out to be that they needed to first fire the 'unmask' event against the text field, and THEN .set the value they wanted, in order for things to work correctly when automating the testing. This doesn't exercise the field masking functionality, but then again I doubt the test mandate in this instance is to extensively test a 3rd party (JQuery) addin, and they are more concerned with the business logic etc in the back end, thus simply being able to set the value without the masking code getting in the way is what is needed.
The problem has to do with focusing on the text field. Even with a .click() somehow webdriver ends up with the cursor at the end if the input field (see issue 2377). Pressing the HOME key moves the cursor to the beginning and allows you to type in the input field, and still have the mask functionality.
In Java:
WebElement txtPhone = getDriver().findElement(By.id("phoneNumbers_value_input"));
txtPhone.sendKeys(org.openqa.selenium.Keys.HOME);
txtPhone.sendKeys(phoneNumber);
I have this input of type "Submit" that Watir cannot see. I can identify it by ID, but it doesn't turn up in browser.text_fields, or by any other idenfication method. The ID is dynamically generated so I cannot use it for testing. Any ideas on how to make this readable? I'm willing to change the WATiR source code if necessary.
<INPUT id=t8CPm value=Submit type=submit>
I have obviously tried text_field(:value, 'Submit') and text_field(:type, 'Submit'), and I get an "Unable to locate element" error.
Did you try treating it as a button element? Inputs of type submit are generally considered to be a button because the browser generally renders them that way.
try
browser.button(:value, 'Submit').flash
and see if it works for you
I changed INPUT_TYPES to ["text", "password", "textarea", "submit"] in the TextField class of input_elements.rb and there it was.
I should also probably edit the collections to read the type too.
Edit: I am an idiot and I didn't need to do this, but I'm leaving it here in case anyone else needs to identify a real dynamic-id custom-type text field, not a fake dynamic-id custom-type text field otherwise known in my particular case as a "button".