Unable to display an 'onclick' attribute - watir-webdriver

I have HTML that looks like this:
<html>
<head></head>
<body>
<a class="xyxyxy" href="" onclick="window.open('https://iogossip.com/Disclosure.pdf', '_blank')">DISCLOSURE</a>
</body>
</html>
I need to put the 'https://iogossip.com/Disclour.pdf' to the screen. or put the whole thing like "window.open('https://iogossip.com/Disclosure.pdf', '_blank')" to the screen.
when I do this:
puts browser.a.onclick
I am getting this error:
NoMethodError: undefined method `onclick' for #<Watir::Anchor:0x29fc448>
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir
-webdriver/elements/element.rb:553:in `method_missing'
from (irb):6
from C:/Ruby193/bin/irb:12:in `<main>'
thanks,

You can get attribute values using the Element#attribute_value method (which should be supported in watir 2.0.3):
puts browser.a.attribute_value('onclick')
#=> "window.open('https://iogossip.com/Disclosure.pdf', '_blank')"

your code worked fine for me.
try:
href="#"

Related

How to use onclick method inside AEM component

Am having a AEM6 html component, am getting the values from dialog and using it inside the component via the .js file and using the return properties.
I could able to get the authored values but it is getting null or empty when am using it inside the onclick method. Please find below the code snippet below.
<div data-sly-unwrap data-sly-use.test="test.js"></div>
<a href="#" class="${test.testId}" id="${test.testId}" onClick="toggleDraw('${test.testId}')" >
The content I authored is getting displayed in class and Id, but it is not displaying in the onClick method.
Below is the Output am getting after authoring.
<a href="#" class="get-a-quote" id="get-a-quote" onClick="toggleDraw('')" >
Output I needed is :
<a href="#" class="get-a-quote" id="get-a-quote" onClick="toggleDraw('get-a-quote')" >
This should do the trick:
<a data-sly-test.variable123="toggleDraw('${test.testId}')" href="#" class="${test.testId}" id="${test.testId}" onclick="${variable123 # context='attribute'}" >
You need to put the function call in a variable because of the nested single quotes. And you need to manually set the context in this case. If "attribute" does some escaping you do not like, you could use "unsafe" - this will end in all escaping mechanisms being disabled. That might or might not be a security issue for your application.
HTH

EJS includes not working

I have an EJS file called 'test.ejs', which contains:
<p>This is a test statement</p>
Then, I have another file called 'index.ejs' which is in the same folder as the other EJS file. Index.ejs contains:
<%- include ("test") %>
<p>Hello world</p>
However, when I load the page, I do not get any output. Instead, the page just loads endlessly.
My routes in Express are being handled properly and my controllers are working too. All this is tested.
Also, can someone refer me to good EJS documentation?
Try this:
<% include ./test %>
<p>Hello world</p>
try: <% include /pathname/test %>
Documentation is sparse but the official one works
http://www.embeddedjs.com/
or
http://ejs.co/

Polymer data bind without dom-bind

I have a polymer element <my-element> with a computed property myProperty. I need to bind myProperty to another place in the HTML page, so I can't put it inside a dom-bind template
Here's what I mean
<html>
<body>
<div>
<my-element my-property="{{myProperty}}"></my-element>
</div>
<!--somewhere deep inside another part of the document-->
<div>
<h4>myProperty = </h4><span>[[myProperty]]</span>
<div>
</body>
</html>
I cannot wrap my-element and the usage of [[myProperty]] in a dom-bind template as this would result in nearly the entire document being enclosed in this. Attempting to use the bind as it is results in myProperty = [[myProperty]] being displayed, not the value of [[myProperty]].
Is there some way to have behaviour similar to data binding but usable across the whole HTML document? (In the future there might also be a case where [[myProperty]] is used inside an attribute such as <my-second-element my-property="[[myProperty]]">). Or if both occurences are wrapped individually in dom-bind templates is there some way to make the bind global?
Thanks in advance
Not sure why you wouldn't be able to do like this:
<head>
...
<script src="bower_components/webcomponentsjs/webcomponents-lite.js"></script>
...
</head>
<html>
<body>
<template is="dom-bind" id="app">
<div>
<my-element my-property="{{myProperty}}"></my-element>
</div>
<!--somewhere deep inside another part of the document-->
<div>
<h4>myProperty = </h4><span>[[myProperty]]</span>
<div>
</template>
</body>
</html>
This is totally doable. If myProperty changes inside my-element it would also change in "this" html-document. There also wouldn't be a problem adding your second element:
<my-second-element my-property="[[myProperty]]">
Unless you're missing to tell us some specific behavior that you want, this should be what you want. :)

New to jquery. Starts with selector breaks the code. what's is wrong?

Thanks for the help. I'm trying to use a jQuery selector to watch for a click on a group of elements, that start with particular characters. I have come up with the following code, but I must be missing something. If I hard code the ID (ie. $("#test_1")...), the code works:
<body>
<div id="content">
<div id="parentcontainer">
<div id="test_1"></div>
</div>
</div>
</body>
<script>
$(window).load(function(){
$("#statusbar").text("Ready");
$("#parentcontainer").click(function(){alert("parent clicked");});
$("#btnaddelement").click(function(){alert("Add Button Clicked");});
$("[name^='test_']").click(function(e){e.stopPropagation();
alert("Child Clicked");});
});
</script>
You are selecting on $("[name^='test_']") which will give you elements who have a name attribute that start with test_. You need to select on $("[id^='test_']") for elements with an id that start with test_. That is one example of what you are getting with your hard-coded success of $('#test_1') -- an element whose id attribute is test_1.
Also, be aware if you are not already that xpath is the language used for selectors, so you can do all kinds of incredible selection if you become familiar with it.
Yes, you missed something. Change the div's attribute id to name will work
<div name="test_1"></div>
Actually, class was used more frequently.
And there are an opinion I want to improve the code.
Try to use the jquery's $(document).ready instead of DOM's load. Because load will wait for all the sources to be loaded compeletely before the js code can be executed, for example, all the photos are downloading ok.
I hope this help!

Can't target iframe with watir-webdriver - Bug?

I am pretty new to automation and watir-webdriver so forgive me if I don't sound super techy.
I am trying to log in to a website and the login form is inside of an iframe. There is also another iframe on the same page that contains an image.
This is the html:
<body>
<div class="topbar">
<div class="topbarcenter">
<ul>
<li id="logo" class="logo">
<div id="provider_logo">
<iframe id="logo_iframe" width="192px" height="128px" frameborder="0" src="http://social.onerecovery.com/modules/iframes/html/provider_logo.html?prov=microsites" onload="this.style.visibility = "visible";" style="visibility: visible;" allowtransparency="true">
</div>
</li>
<li class="login">
<iframe id="login_iframe" width="550px" height="70px" frameborder="0" src="http://social.onerecovery.com/modules/iframes/html/login.html" onload="this.style.visibility = "visible";" style="visibility: visible;" allowtransparency="true">
<html>
<head>
<body>
<div class="login_container">
<div id="login_div">
<form class="login_form" action="#" method="post">
<input type="text" maxlength="100" placeholder="Email Address..." class="email_input processed" name="email">
by the way I am using watir-webdriver 0.3.5 and automating on chrome 17
what I tried was:
$b.frame(:id => "login_iframe").form(:class => "login_form").text_field(:name => "email").set("username")
which I thought would work but in my command line I just get the error: Watir::Exception::UnknownObjectException: unable to locate element, using {:class=>"login_form", :tag_name=>"form"}
I also tried indexing the iframe to make sure I was in the second iframe and not the first but it still didn't work.
When I do
$b.frame(:id => "login_iframe").exists? in command line, I get
true
but when I do
$b.frame(:id => "login_iframe").form(:class => "login_form").exists? in command line, I get
false
The thing is that we have another page that someone can use to login to the same website and the only difference between that page and this page is that this page has a second iframe whereas the other page only has the login iframe and the code
$b.frame(:id => "login_iframe").form(:class => "login_form").text_field(:name => "email").set("username")
works perfectly fine.
Sorry for going on so long. Just wanted to make sure that I gave enough info. Thanks in advance for any help.
The short answer to solving your problem is to use browser.frame(:index => 2) instead of browser.frame(:id => "login_iframe").
Or if you want a slightly more robust solution:
frame = browser.frames.find{ |frame| frame.form(:class => "login_form").exists? }
frame.form(:class => "login_form").text_field(:name => "email").set("username")
That said, I really do not know why that works. It is like it thinks the login control is inside the invite_iframe, which it does not look like in the HTML. I will try to dig deeper, but sounds like a bug to me.
It appears that all of the iframes are shuffled funny. As you can see by the following, the number of text fields in each iframe does not match what is expected.
browser.frames.each{ |x| puts x.id + ' - ' + x.text_fields.length.to_s + ' text_fields' }
#=> logo_iframe - 3 text_fields
#=> login_iframe - 0 text_fields
#=> invite_iframe - 2 text_fields
For the latest versions, it is browser.iframes which lists all the iframes current window has.
browser.iframes.map {|iframe| iframe.src}
This will map src attributes of all iframes.