Using findElement in groovy pages - groovy

Apologies if this question seems trivial,.. but I am very very new to groovy/selenium as part of a maven system I’ve been thrown at and am in need to understand what I am missing in trying to get this method to work.
The error I am getting is below:
groovy.lang.MissingMethodException: No signature of method: GebConfig.findElement() is applicable for argument types: (org.openqa.selenium.By$ByName)
I’m in need to locate elements on a web page and would like to use the findElement method, however my code is in groovy as part of step definitions.
I have ended up with the following after lots of attempts but am getting nowhere:
package step_definitions
import features.support.Requests
import geb.*
import org.apache.commons.io.*
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.*
import org.openqa.selenium.remote.*
import cucumber.api.groovy.EN.*
When(~/^find the element named "(.*?)"$/) { String btnName ->
WebElement myElement = driver.findElement(By.name(btnName));
}
I know I can use things like the below for a button and similar for other things like radio buttons and input fields:
browser.$(‘input’, name: ‘btnK’)
$(‘input’, name: ‘btnK’)
But I’d prefer to know how to use the findElement approach.
Any help would be appreciated.
Thanks,
Jim…..

I can see that you are using Geb with Cucumber JVM. If you've setup your environment using geb.binding.BindingUpdater as described in http://gebish.org/manual/current/#writing-your-own-steps then the methods and properties available in your steps are as listed in http://gebish.org/manual/current/#browser-methods-and-properties. You will notice that there is no driver property in that list - if you wish to access the driver instance then you will have to obtain it from browser:
When(~/^find the element named "(.*?)"$/) { String myName ->
WebElement myElement = browser.driver.findElement(By.name(btnName));
}

Related

jmeter: access testplan name in sampler

We are developing some test cases using JSR-223 samplers ( groovy lanquage). We have some configuration information that is loaded at run time that keys off the TestPlan name. I don't see a means for accessing the testplan or name from the sampler, or sampler result. Is there a means to do so?
Here it is:
import org.apache.jmeter.services.FileServer;
String script = FileServer.getFileServer().getScriptName();
You can do the same using __TestPlanName() function passing via "Parameters" section
References:
FileServer class JavaDoc
How to Use JMeter Functions
UPDATE
Theoretically it is possible to access JMeter Test Plan tree, but remember, every time you bypass Java limitation using Reflection somewhere somehow a kitten dies.
Example code:
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.SearchByClass;
import java.lang.reflect.Field;
import java.util.Collection;
StandardJMeterEngine engine = ctx.getEngine();
Field test = engine.getClass().getDeclaredField("test");
test.setAccessible(true);
HashTree testPlanTree = (HashTree) test.get(engine);
SearchByClass<TestPlan> testPlans = new SearchByClass<>(TestPlan.class);
testPlanTree.traverse(testPlans);
Collection<TestPlan> testPlansRes = testPlans.getSearchResults();
for (TestPlan testPlan : testPlansRes) {
log.info(testPlan.getProperty("TestElement.name").toString());
}
Demo:
You can check out How to Use BeanShell: JMeter's Favorite Built-in Component for more information on using JMeter and Java API.

Access tied files through groovy in jenkins

I'm building a Jenkins plugin, and am handling the UI components using Groovy. In jelly, you can use "${it.something}" to access information in the java file tied to the jelly file, as shown here:
class:
public String getMyString() {
return "Hello Jenkins!";
}
jelly:
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">
${it.myString}
</j:jelly>
from https://wiki.jenkins-ci.org/display/JENKINS/Basic+guide+to+Jelly+usage+in+Jenkins.
I'd like to do the same thing in groovy, but can't seem to find an example of how it's done. Any examples?
After even more searching and some luck, I found the correct way to do this. If I was to use the class in my question but wanted to use groovy instead of jelly, the groovy code would look like this (this puts the string in the textbox):
package something.something;
import lib.JenkinsTagLib
import lib.FormTagLib
def f = namespace(lib.FormTagLib)
t=namespace(JenkinsTagLib.class)
f.entry(title:"text", field:"text") {
f.textbox(value:instance?.text)
}

Groovy Grape dealing with dependency resolution

I am trying to use org.xhtmlrenderer:core-renderer:R8pre2 in a groovy script, but I get a Linkage error:
Caught: java.lang.LinkageError: loader constraint violation in interface
itable initialization: when resolving method
"org.apache.xerces.dom.NodeImpl.getOwnerDocument()Lorg/w3c/dom/Document;"
the class loader (instance of org/codehaus/groovy/tools/RootLoader) of the
current class, org/apache/xerces/dom/NodeImpl, and the class loader (instance of
<bootloader>) for interface org/w3c/dom/Node have different Class objects for
the type getOwnerDocument used in the signature
I've already googled a lot and found a lot of answer like these:
Dealing with "Xerces hell" in Java/Maven?
XercesImpl in conflict with JavaSE 6's internal xerces implementation. Both are needed... what can be done?
So, one solution could be to use javas endorsed mechanism to resolve the conflict, but I would like to make my script independent from such a "workaround". The script should run out of the box.
Next thing I was giving a try was to exclude the right dependency like this
#Grapes([
#Grab('org.xhtmlrenderer:core-renderer:R8pre2'),
#GrabExclude('xml-apis:xml-apis')
])
but didn't succeed...
Any ideas?
PS: here is the script which creates the error:
#Grapes([
#Grab('org.xhtmlrenderer:core-renderer:R8pre2'),
])
import org.w3c.dom.Document
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
def dbf = DocumentBuilderFactory.newInstance()
DocumentBuilder builder = dbf.newDocumentBuilder()
Document doc = builder.parse(new ByteArrayInputStream("<html></html>".getBytes()))
Thanx to #dmahapatro, I checked my configuration and found that I dropped some jars in {usrhome}/.groovy a long time ago. Removed these and now everything work like a charm...

Geb drive method

I'm trying to learn how to use Geb and I'm getting an error. Could you guys help me out?
I'm trying to use the drive method but it is not working. I've tested a few of the other Browser's methods and they work all right. Just the drive method is giving me trouble.
I've checked the API and googled around but didn't find anything helpful. The strange thing is the fact that I don't get an error message. There is no Exception. I'm running the code on Groovy's console and Firefox just chills for a while and then the execution finishes.
Geb 0.9.2, FirefoxDriver and JDK 7
import org.openqa.selenium.WebDriver;
import geb.Browser
import org.openqa.selenium.firefox.FirefoxDriver
public class MyTest {
Browser browser;
void test(){
browser = new Browser(driver: new FirefoxDriver())
browser.go "http://www.google.com" // this works
browser.$("div button", name: "btnK").text() == "Google Search" // this works
browser.drive { // WHY U NO WORK?!!
go "http://www.google.com"
}
}
}
x = MyTest()
x.test()
You should know that drive() is a static method and it's designed to be used in scripts where you don't instantiate a browser instance. You have to decide - you either use a browser instance or the Browser.drive {} method. Yo cannot do both.
You might also consider using one of the integrations with testing frameworks - by doing so you'll get Geb to manage a browser instance for you.

What is the best way to import constants into a groovy script?

I have been setting up a scripting envrionment using Groovy. I have a groovy script called FrameworkiDatabase.groovy which contains a class of the same name. This works fine. I also have another file called connections.groovy which contains maps like the following:
SUPPORT2=[
host:"host.name",
port:"1521",
db:"support2",
username:"username",
password:"password",
dbType:"oracle"
]
This holds a collection of database bookmarks, a bit like an oracle tnsnames file, so I don't need to remember all the parameters when connecting to databases.
When using groovysh, I can import this using the load command, and it is available in current scope. How can I load it as part of a script the same way? It has no class definition around it - does it need one? I have tried doing that, and adding a static import, but that didn't work...
I tried something like this, but no luck:
testFrameworkiDatabase.groovy:
import static connections
def db = new FrameworkiDatabase(SUPPORT2)
db.listInvalidObjects()
db.getDBSchemaVersion()
db.getFWiVersion()
db.getSPVersion()
db.getFileloaderVersion()
db.getAdminToolVersion()
db.getReportsVersion()
So I want to load those connections as constants - is there any way I can do this easily?
Not sure if it's the best way, but one way would be to write this into Connections.groovy
class Connections {
static SUPPORT2 = [
host:"host.name",
port:"1521",
db:"support2",
username:"username",
password:"password",
dbType:"oracle"
]
}
Then, compile this with groovyc Connections.groovy to generate a class file
Then, in your test script or on the groovysh prompt, you can do:
import static Connections.*
println SUPPORT2
To get the output:
[host:host.name, port:1521, db:support2, username:username, password:password, dbType:oracle]
If compiling the Connections.groovy class isn't good enough, I think you're going to be looking at loading the source into a Binding object by using one of the Groovy embedding techniques

Resources