in feature file when I want to pass multiple condition in xpath then I am getting selenium.common.exceptions.InvalidSelectorException
Feature: showing off behave
Scenario Outline: run a simple test
Given we have behave installed <thing> and <good>
And Enter value in google <x_path> and <g_val>
Examples:
| thing | good | x_path | g_val |
| Red Tree Frog | Hello Automation | //input[#name='q' and #class='gsfi'] | hello world |
The StepDefination code is as below -
from behave import given
from selenium import webdriver
driver = webdriver.Ie("C:\\Users\\ksahu\\Downloads\\IEDriverServer_x64_3.3.0\\IEDriverServer.exe")
driver.implicitly_wait(15)
#given(u'we have behave installed {thing} and {good}')
def step_impl(context,thing,good):
print('============> '+thing+'===========> '+good)
#given(u'Enter value in google {x_pth} and {g_val}')
def step_impl(context,x_pth,g_val):
driver.get("http://www.google.com")
driver.maximize_window()
print(x_pth)
#driver.find_element_by_name(x_pth).send_keys(g_val)
driver.find_element_by_xpath(x_pth).send_keys(g_val)
The Exception is as below -
selenium.common.exceptions.InvalidSelectorException: Message: Unable to locate an element with the xpath expression //input[#name='q'\ and\ #class='gsfi'] because of the following error:
Error: Bad token, expected: ] got: \
Captured stdout:
=============> Red Tree Frog==========> Hello Automation
//input[#name='q'
The SPACE is not allowing in xpath.
Please let me know how to pass multiple condition in xpath in feature file
One quick workaround would be to specify multiple conditions this way:
//input[#name='q'][#class='gsfi']
Or, I think, putting the placeholder into backticks should solve the space escaping issue:
#given(u'Enter value in google `{x_pth}` and {g_val}')
Related
I'm working on a project which uses a combination of Windows, Java, Groovy, Gradle and Cucumber. This combination gives me some problems on my Windows machine that my *NIX colleagues are not experiencing. Upon running the gradle build, gradle wants to output some reports. The location and filename of these reports is apparently determined by the definition or output of the Cucumber tests. The name used is unfortunately not something that can be used as a filename, so I'm getting an IOException for each test report.
For the Cucumber test, we use the following structure:
Scenario Outline: Receive and parse ReturnItem from Service
Given The message from service return item outlined in <messagePath>
When We process the message
Then XXX posted a message to YYY on topic <topic> with event <eventType>
And payload matches <resultPath>
| messagePath | topic | eventType | resultPath |
| /test/testxml.xml | test_topic | EVENT_TYPE | /result/result.json |
After running this, I receive the following exception:
Caused by: org.gradle.api.UncheckedIOException: Could not write to file 'C:\xxx\project\build\reports\tests\test\packages\| \test\testxml.xml | test_topic | EVENT_TYPE | \result\result.html'.
at org.gradle.internal.IoActions$TextFileWriterIoAction.execute(IoActions.java:151)
at org.gradle.internal.IoActions$TextFileWriterIoAction.execute(IoActions.java:127)
at org.gradle.internal.IoActions.writeTextFile(IoActions.java:45)
at org.gradle.reporting.HtmlReportRenderer$DefaultHtmlReportContext.renderHtmlPage(HtmlReportRenderer.java:118)
at org.gradle.api.internal.tasks.testing.report.DefaultTestReport$HtmlReportFileGenerator.run(DefaultTestReport.java:147)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:300)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:292)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:174)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.access$900(DefaultBuildOperationExecutor.java:48)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$ParentPreservingQueueWorker.execute(DefaultBuildOperationExecutor.java:342)
at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable.runOperation(DefaultBuildOperationQueue.java:230)
at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable.access$600(DefaultBuildOperationQueue.java:172)
at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable$1.call(DefaultBuildOperationQueue.java:209)
at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable$1.call(DefaultBuildOperationQueue.java:203)
at org.gradle.internal.work.DefaultWorkerLeaseService.withLocks(DefaultWorkerLeaseService.java:154)
at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable.runBatch(DefaultBuildOperationQueue.java:203)
at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable.run(DefaultBuildOperationQueue.java:177)
... 3 more
Caused by: java.io.IOException: Unable to create directory 'C:\xxx\project\project-test\build\reports\tests\test\packages\| \test\testxml.xml | test_topic | EVENT_TYPE | \result'
at org.gradle.internal.IoActions$TextFileWriterIoAction.execute(IoActions.java:141)
... 19 more
Does anybody know how to fix this? The only 'solution' I could come up with is disabling the reports, which works, but is more a workaround than a solution. For disabling I used the following configuration in the gradle.build for this:
apply plugin: 'java'
apply plugin: 'groovy'
test {
reports {
junitXml.enabled = false
html.enabled = false
}
}
(Inspired by: How to make Gradle build produce HTML test report instead of XML default?)
I finally found the culprit. Apparently these filenames correspond to the default behaviour of JUnit for report generation of Cucumber tests. On *NIX, this doesn't provide any problem. On Windows however, this will result in an exception due to the pipes in the Examples. The IOException is somewhat special apparently, since the most exceptions that I found on the internet were FileNotFoundExceptions. That explains why it took me so long to find an answer, I focused on the exception.
The solution here is to use the following JUnitOption as an #CucumberOptions annotation when running the Cucumber tests: --filename-compatible-names.
A code example for Java & Spring looks like this:
#RunWith(Cucumber.class)
#CucumberOptions(junit = {"--filename-compatible-names"})
public class CucumberRunner {
}
It would be nice if these kind of non-breaking OS dependent options would be default instead of optional.
Based upon the information provided, it looks like it's trying to create a directory called:
'C:\xxx\project\project-test\build\reports\tests\test\packages\| \test\testxml.xml | test_topic | EVENT_TYPE | \result'
Can you show the code around passing in messagePath? I suspect you are passing in the entire row of data rather than just the messagePath (I'm going to take a wild guess that you are performing a .toString() on an array instead of passing in the first element of the array)
I'd like to run an entire file with JShell like:
$ jshell my-jshell-skript.java
Where e.g. the content of my my-jshell-skript.java is 40 + 2;.
Or alternatively an executable like:
#!/usr/bin/jshell
40 + 2
Is this possible now or do I still have to take the old way over a Java-Main-Class?
Edit 1: Windows-Problem
On Windows, there is still no solution for me:
C:\JDKs\jdk9.0.0.0_x64\bin>type foo.jsh
1 + 1
C:\JDKs\jdk9.0.0.0_x64\bin>jshell.exe foo.jsh
| Welcome to JShell -- Version 9
| For an introduction type: /help intro
jshell> /exit
| Goodbye
C:\JDKs\jdk9.0.0.0_x64\bin>
JShell starts ignoring my file completely. Is it a bug?
Edit 2: Solution for Windows-Problem
Turns out that it is the content of my foo. Seems like 1 + 1 does only work "on the fly", not read from a file:
C:\JDKs\jdk9.0.0.0_x64\bin>type foo.jsh
System.out.println("foo");
C:\JDKs\jdk9.0.0.0_x64\bin>jshell.exe foo.jsh
foo
| Welcome to JShell -- Version 9
| For an introduction type: /help intro
jshell> /exit
| Goodbye
C:\JDKs\jdk9.0.0.0_x64\bin>
You can create a Jshell script file named some.jsh with those statements and on the command prompt from where you run jshell, execute it as:-
jshell /path/to/some.jsh
On a MacOSX, I would do something like:
You can pipe the string to JShell:
echo 1 + 2 | jshell
Example:
:/# echo 1 + 2 | jshell
| Welcome to JShell -- Version 9
| For an introduction type: /help intro
jshell> 1 + 2
$1 ==> 3
:/#
Or, from a file:
cat myfile | jshell
Where myfile contains the line "1 + 2".
JShell is not meant to run a Java class directly. If you want to run a java class, you still need to do it the old way - java <your-class-name>.
From the docs,
The Java Shell tool (JShell) is an interactive tool for learning the
Java programming language and prototyping Java code. JShell is a
Read-Evaluate-Print Loop (REPL), which evaluates declarations,
statements, and expressions as they are entered and immediately shows
the results.
As per this quote, JShell is meant for running or trying out individual Java statements. In the traditional java way, you have to write a full Java program before you can run it and see the results. But JShell allows you a way to try out the Java statements without needing you to build the full standalone java application.
So the short answer to your question is that, no, you can't call standalone java applications like jshell my-jshell-skript.java. However, you CAN call a script file which contains individual JShell commands or Java statements. So if you copy all the statements from your Java program and paste them to a JShell script, you can run the script like:
% jshell my-jshell-skript.jsh
But this is not quite the same as running a standalone java application.
Launch jshell in concise feedback mode and filter the required content-
$echo '40 + 2' | jshell --feedback concise | sed -n '2p' |sed -En 's/[^>]*>(.+)/\1/gp'
output: 42
More details here- How to execute java jshell command as inline from shell or windows commandLine
In jshell you can save the current snippets into a file by issuing:
/save Filename
Likewise, you can load the file into the current context/session by issuing:
/open Filename
Here is one such example:
| Welcome to JShell -- Version 9.0.7.1
| For an introduction type: /help intro
jshell> String[] names={"nameone","nametwo"}
names ==> String[2] { "nameone", "nametwo" }
jshell> Arrays.toString(names);
$2 ==> "[nameone, nametwo]"
jshell> /save myExample
jshell> % sudipbhandari at sysadm-Latitude-5480 in ~ 18:22
> jshell
| Welcome to JShell -- Version 9.0.7.1
| For an introduction type: /help intro
jshell> names
| Error:
| cannot find symbol
| symbol: variable names
| names
| ^---^
jshell> /open myExample
jshell> names
names ==> String[2] { "nameone", "nametwo" }
In Windows, to see the verbose output for a jsh file
type file.jsh | jshell -v
Problem when running jshell file.jsh
D:\>type file.jsh
3 + 5
D:\>jshell file.jsh
| Welcome to JShell -- Version 13.0.2
| For an introduction type: /help intro
jshell>
Workaround:
D:\>type file.jsh
3 + 5
D:\>type file.jsh | jshell -v
| Welcome to JShell -- Version 13.0.2
| For an introduction type: /help intro
jshell> $1 ==> 8
| created scratch variable $1 : int
jshell>
Note: file should contain a blank line (/n) after the last line, else the last line is not getting executed
Pipe usage can be achieved with the "hyphen" option, absent in the initial jshell release.
echo 'System.out.print(1 + 2)' | jshell -
https://docs.oracle.com/en/java/javase/11/tools/jshell.html
https://bugs.openjdk.java.net/browse/JDK-8187439
I'm having some trouble when using exec("pgrep java",$output);.
On the terminal I execute the pgrep java and it returns the Process ID, but when I use the same command on PHP it doesn't outputs anything, I was thinking about permissions but I checked that PHP current user it's root.
I also tried ps ax | grep java and the outputs is this:
But when I use the terminal the output it's different:
The same happens when using ps aux | awk '/java/ {print $2}'
On PHP: Array ( [0] => 14970 [1] => 14972 )
On Terminal: 14084 & 14963
I also would like to point out that everytime I refresh the page the PIDs that are get are new, meaning that they're not constant, this also happens with one PID on the terminal, but the 14084 is the one that it's constant.
What's the best way to use pgrep? Or is there another way to get the process ID?
It seems that when deploying my Spring app to AppFog, that the environment variables are not being detected.
I have the environment variables declared in my console:
And I try to reference these from within my app code, like so:
<context:property-placeholder
location="classpath:server.common.properties,
classpath:server.${concorde-env}.properties"/>
However, this generates an error:
Caused by: java.io.FileNotFoundException: class path resource
[server.${concorde-env}.properties] cannot be opened because it does
not exist
This approach works fine in other (non AppFog) environments.
I logged out the properties by calling:
log.info("Properties: " + System.getProperties().toString());
And it doesn't show those properties as available.
However, if I do a af env <<MY_APP_NAME>>, it shows the variables as present:
+--------------------------+------------------+
| Variable | Value |
+--------------------------+------------------+
| concorde-env | test |
| spring.profiles.active | runtime,test |
+--------------------------+------------------+
What am I missing to make these variables exposed to my app at runtime?
Try accessing the value like this: System.getenv("concorde-env") to see if the environment var can even be accessed in code.
Based on the error message "class path resource [server.${concorde-env}.properties] cannot be opened because it does not exist" it seems like ${concorde-env} is not even being evaluated or replaced even with empty string.
It looks like Spring has other ways of accessing env vars. Try #{systemEnvironment['concorde-env']} instead of ${concorde-env}
Is there a good interactive interpreter for Java, similar to Scala's? When programming, I like to try small pieces of code to see if they work like I expect before plugging them in to my main program. I would prefer an interpreter that is not based online since I often work offline.
Thanks in advance!
Since Scala runs on a JVM, why not the Scala interpreter?
That's what I do when I need to test a few Java-snippets.
It's nice to have tab completion that works.
scala> System.getPropert
getProperties getProperty
scala> System.getProperty("user.home")
res0: String = c:\Users\robert
Ok, you have to know a bit about Scala syntax when you do stuff like:
scala> val testMap = new java.util.HashMap[String, java.util.List[String]]
testMap: java.util.HashMap[String,java.util.List[String]] = {}
scala> testMap.put("planets", java.util.Arrays.asList("Mars", "Jupiter", "Pluto"
))
res0: java.util.List[String] = null
scala> testMap.get("planets")
res1: java.util.List[String] = [Mars, Jupiter, Pluto]
In the past I used beanshell. It was really light and got the job done. http://www.beanshell.org/
You can use Dr. Java. It has an interactions window that acts like an interpreter. I don't know how they developed it though, so, you better use it cautiously. E.g. if you have a class with a private method, Dr. Java's interactions will let you use this method by instance.
I used BlueJ a bit in college. It was started as a teaching tool, it's perfect for tinkering with code and the project is (was?) supported by Sun so I guess that's a feather in its cap too. It's been a while since I've used it but the nice thing I remember about it is that you can tinker with the code you write without writing a main() class. You can write a class, create an instance, call methods (it will prompt you for parameters) and so on. There's also a visual aspect to it as well, somewhat comparable to the inspector in Eclipse.
http://www.bluej.org/about/what.html
Since Java 9 the JDK comes with it's own REPL, the jshell.
If the java/bin directory is on the PATH you should be able to start it from the command line.
$ jshell
| Welcome to JShell -- Version 11.0.14
| For an introduction type: /help intro
jshell> /imports
| import java.io.*
| import java.math.*
| import java.net.*
| import java.nio.file.*
| import java.util.*
| import java.util.concurrent.*
| import java.util.function.*
| import java.util.prefs.*
| import java.util.regex.*
| import java.util.stream.*
jshell> 1 + 1
$1 ==> 2
jshell> int foo(int x) {
...> return x + 1;
...> }
| created method foo(int)
jshell> foo(5)
$3 ==> 6
You can see the current imports by calling /imports.
Jshell starts with default imports but you can always import more classes as long as they are on the classpath.
You can start the jshell and provide a classpath argument:
$ jshell --class-path /opt/libs/guava-19.0.jar:/opt/libs/commons-lang3-3.4.jar
See more regarding classpath options in this answer.