I want to write some selenium script using jp#gc - WebDriver Sampler . Here I used Script Language JAVA. Can Here I used main function ? or is it supported only JUnit? Can I write a script in TestNG?
Already I wrote one script for that I got error" when used (driver.get("https://opensource-demo.orangehrmlive.com"); )
java.net.MalformedURLException: unknown protocol: data
at java.net.URL.<init>(URL.java:618)
at java.net.URL.<init>(URL.java:508)
at java.net.URL.<init>(URL.java:457)
at com.googlecode.jmeter.plugins.webdriver.sampler.WebDriverSampler.sample(WebDriverSampler.java:90)
at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:638)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:558)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256)
at java.lang.Thread.run(Thread.java:750)
There is no java language either in WebDriver Sampler or in JSR223 Test Elements, when you choose java in the language drop-down the script will be evaluated by Beanshell interpreter. If you're looking for the closest to Java syntax - go for groovy
There is no need to define an "entry point", the interpreter reads the script at once and executes line-by-line. You can have classes if you want. You can have main() function if you want but you will need to explicitly call it.
There a pre-defined WebDriver instance WDS.browser so the minimal test would be something like:
WDS.sampleResult.sampleStart()
WDS.browser.get('http://example.com')
WDS.sampleResult.sampleEnd()
You may find The WebDriver Sampler: Your Top 10 Questions Answered article useful.
I have a Groovy switch called (very) frequently in my application. VisualVM sampling shows that I spend around 20% of my application time in ScriptBytecodeAdapter.isCase().
Since all the cases are strings, my usage would be supported by the standard Java 7 string switch which should be more efficient.
Is it possible to ask Groovy to fallback to the standard Java switch ?
Remarks:
I can use ASTs (the switch it self is generated by an AST in SEMANTIC_ANALYSIS).
Other implementation options could be to use ifs/elses instead (I usually have < 20 cases) or a Map of Closures
Applying the Groovy CompileStatic transformation in your AST, after building your nodes, should cause your generated code to be statically compiled. Be sure that you are doing this programmatically in your code, not by adding the annotation to your transformation class.
In most cases the plain old Java version of code constructs will be more performant than the added Groovy syntax; however, it can be difficult to access the underlying Java constructs in some cases.
This should at least take it one step farther.
I'm working on some JMeter test plans that employ BeanShell assertions. Within these assertions I want to acess some user properties. There are several ways to access them:
JMeterUtils.getProperty("propertyName")
${__P(propertyName)
props.get("propertyName")
Where are the differences and what are the pros and cons of each option? Are they wrapper of each other or do they have specific functionality?
Thank you!
Functionally they are all exactly the same. They are even implemented the same way - they all call the getProperty() method on the current jmeter properties object.
The difference is where you use them.
The ${} notation is used when putting variables into JMeter GUI text boxes. In the fields on the HTTP sampler for example. note that __P is shorthand for, and exactly the same as __Property
props.get() is used in beanshell scripts, without having to explicitly import JMeterUtils. You can also combine 1&2 to do ${__BeanShell(props.get())}
If you do import JMeterUtils in beanshell, or you're developing a custom java class, then you would use JMeterUtils.getProperty().
Of the three, I would think #1 is the most efficient because it doesn't need to instantiate and evaluate beanshell
For Beanshell feel free to use any approach you like.
For JSR223 Test Elements and Groovy language which is recommended way of doing scripting in your JMeter test - avoid refering JMeter Variables and Functions using ${this way} as it prevents script from compilation and causes execution overhead. See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! article for more detailed explanation, different scripting approaches benchmark and scripting best practices.
I am aware that by default Java does not have the so-called eval (what I pronounce as "evil") method. This sounds like a bad thing—knowing you do not have something which so many others do. But even worse seems being notified that you can't have it.
My question is: What is solid reasoning behind it? I mean, Google'ing this just returns a massive amount of old data and bogus reasons—even if there is an answer that I'm looking for, I can't filter it from people who are just throwing generic tag-words around.
I'm not interested in answers that are telling me how to get around that; I can do that myself:
Using Bean Scripting Framework (BSF)
File sample.py (in py folder) contents:
def factorial(n):
return reduce(lambda x, y:x * y, range(1, n + 1))
And Java code:
ScriptEngine engine = new ScriptEngineManager().getEngineByName("jython");
engine.eval(new FileReader("py" + java.io.File.separator + "sample.py"));
System.out.println(engine.eval("factorial(932)"));
Using designed bridges like JLink
This is equivalent to:
String expr = "N[Integrate[E^(2 y^5)/(2 x^3), {x, 4, 7}, {y, 2, 3}]]";
System.out.println(MM.Eval(expr));
//Output: 1.5187560850359461*^206 + 4.2210685420287355*^190*I
Other methods
Using Dijkstras shunting-yard algorithm or alike and writing an expression evaluator from scratch.
Using complex regex and string manipulations with delegates and HashMultimaps.
Using Java Expressions Library
Using Java Expression Language
Using JRE compliant scripting language like BeanShell.
Using the Java Assembler and approach below or direct bytecode manipulation like Javaassist.
Using the Java Compiler API and reflections.
Using Runtime.getRuntime().exec as root
"eval" is only available in scripting languages, because it uses the same interpreter that runs the rest of the code; in such languages the feature is free and well integrated, as in scripting environment it makes little difference if you run a string or a "real" function.
In copiled languages, adding "eval" would mean bundling the whole compiler - which would defy the purpose of compiling. No compiled language I know (even dynamic ones, like ActionScrip3) has eval.
Incidentally, the easiest way to eval in Java is the one you forgot to mention: JRE 1.6 comes with Javascript engine, so you can eval any Javascript in two lines of code. You could even argue that the presuposition of your question is false. Java 1.6 bundles a very advanced expression evaluator.
As Daniel points out there is at least one limitation that eval-solutions face in java. The php eval for example executes the code as if it was part of the surrounding method with complete access to local variables, this is not possible to do in standard java. Without this feature eval alternatives require a lot more work and verbosity, which makes them a lot less attractive for "quick" and "easy" solutions.
eval() is mostly part of interpreted languages where the names of local variables and code structure(scopes) are available at runtime, making it possible to "insert" new code. Java bytecode no longer contains this information leaving eval() alternatives unable to map access to local variables. (Note: I ignore debug information as no program should rely on it and it may not be present)
An example
int i = 0;
eval("i = 1");
System.out.println(i);
required pseudocode for java
context.put("i",new Integer(0));
eval(context,"i = 1");
System.out.println(context.get("i"));
This looks nice for one variable used in the eval, try it for 10 in a longer method and you get 20 additional lines for variable access and the one or other runtime error if you forget one.
Because evaluation of arbitrary Java expressions depends on the context of it, of variable scopes etc.
If you need some kind of variable expression, just use the scripting framework, and badamm! you have lots of different kinds of expression evaluation. Just take one kind like JavaScript as a default, and there is your eval()!
Enterprisy as Java is, you are not constrained to one choice.
But even worse seems being notified that you can't have it.
I think you are misunderstanding what (most of) those articles are saying. Clearly, there are many ways to do expression evaluation in a Java application. They haven't always been available, but at least some of them have been around for a long time.
I think what people are trying to say is that expression evaluation is not available as native (i.e. as an intrinsic part of Java or the standard libraries) and is unlikely to be added for a number of good reasons. For example:
Native eval would have significant security issues if used in the wrong place. (And it does for other languages; e.g. you shouldn't use eval in Javascript to read JSON because it can be a route for injecting bad stuff into the user's browser.)
Native eval would have significant performance issues, compared with compiled Java code. We are talking of 100 to 10,000 times slower, depending on the implementation techniques and the amount of caching of "compiled" eval expressions.
Native eval would introduce a whole stack of reliability issues ... much as overuse / misuse of type casting and reflection to.
Native eval is "not Java". Java is designed to be a primarily static programming language.
and of course ...
There are other ways to do this, including all of the implementation approaches that you listed. The Java SE platform is not in the business of providing every possible library that anyone could possibly want. (JRE downloads are big enough already.)
For these reasons, and probably others as well, the Java language designers have decided not to support expression evaluation natively in Java SE. (Even so, some expression support has officially made it into Java EE; e.g. in the form of JSP Expression Language. The classes are in the javax.el package ... or javax.servlet.jsp.el for an older / deprecated version.)
I think you already put the solution to your answer - bundle the BeanShell jar with your application (or lobby for it to be included in the JRE sometime), and you have your Java expression evaluator. It will still need a Binding of the input variables, though.
(What I'm more curious about: How does sandboxing of such a script/expression work? I don't want my web users to execute dangerous code in my server.)
I have started to learn groovy by building a pet project. I fetch some html with XmlSlurper and parse it etc. I am using eclipse3.4 with groovy 1.6 plugin. I am having a very difficult time trying to iterate thorugh all the html elements etc. I expected to set some breakpoint, inspect the current variable where my contents are, see what it contains, what do I have to iterate over, evaluate some expressions etc etc.
But I almost cannot do anything like that:
- some variables don't appear in the variables view (maybe its the ones not having a type?)
- select any expression but you cannot evaluate
- and worst of all (for me) is that any variable is shown with all its groovy stuff (metaclass, value...). The things that most of the time will interest the developer are buried inside the hierarchy and very difficult to find.
I had thougth that the ObjectExplorer mentioned in the doco would be able to help but I was unable to get it running with my script.
What do people use for this sort of thing while developing in groovy?
Option 1:
Give following a try in your script
groovy.inspect.swingui.ObjectBrowser.inspect(object)
This gives all public fields, properties, methods, etc
Option 2:
You can also use obj.dump() and or object.inspect() method to see values of the object
e.g. println obj.inspect() or assert obj.inspect() == "some values"
Other options:
Eclipse 3.4 debug perspective works pretty well. Even the one without type information show up. Can you give specific problem that you are facing with debugging in 3.4
println variables
Writing Unit test with asserts regarding expected output of the xml