When I use the Playwright's codegen feature it traces my clickpath into a Java file. But the created file has the wrong syntax, so I can't compile it.
I start the codegen with:
mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="codegen wikipedia.org"
And the inspector provides this code:
public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
.setHeadless(false));
BrowserContext context = browser.newContext();
page.navigate("https://en.wikipedia.org/wiki/Main_Page");
page.getByPlaceholder("Search Wikipedia").click();
page.getByPlaceholder("Search Wikipedia").fill("stackoverflow");
page.getByRole("button", new Page.GetByRoleOptions().setName("Go")).click();
assertThat(page).hasURL("https://en.wikipedia.org/wiki/Stack_Overflow");
}
}
}
But there is already the first error. The method getByRole requires an AriaRole as its first parameter, not a String. So it's easy to fix, but I think it's not the idea of the product to generate code and let the developer fix it.
In some YouTube tutorials the inspector generates only fill and click functions with powerful selectors inside.
Is there a way to change the generated output to a specifc "code-style"? Or is there another reason why other people get nice working code and I don't?
My dependency:
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.27.0</version>
</dependency>
Sorry if I am wrong. But you should get something like this from an inspector which compiles fine
package org.example;
import com.microsoft.playwright.*;
import com.microsoft.playwright.options.*;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
import java.util.*;
public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
.setHeadless(false));
BrowserContext context = browser.newContext();
// Open new page
Page page = context.newPage();
// Go to https://www.wikipedia.org/
page.navigate("https://www.wikipedia.org/");
// Click input[name="search"]
page.locator("input[name=\"search\"]").click();
// Fill input[name="search"]
page.locator("input[name=\"search\"]").fill("stackoverflow");
// Click button:has-text("Search")
page.locator("button:has-text(\"Search\")").click();
assertThat(page).hasURL("https://en.wikipedia.org/wiki/Stack_Overflow");
}
}
}
Related
When the rtu.smallview.xhtml action event is triggered it requests info from the java bean, from the database select and hands it back to the xhtml.
The xhtml was not displaying the data from the database, so I added breakpoints in the java bean to figure out what was going wrong, but when the program loaded it never hit the breakpoint in the bean.
The server output is saying this when the program is loaded:
Info: WELD-000119: Not generating any bean definitions from Beans.RTU.RTU_SmallView_Bean because of underlying class loading error: Type pojo.rtu.RTU_unit not found. If this is unexpected, enable DEBUG logging to see the full error.
So I stopped the server, clean and built the project again, and when it runs for the first time it loads the bean, the information is retrieved and displayed. Though if I clean and build the project again, when it runs the second time it displays the same WELD-000119 error.
I copy and pasted just the code to make the RTU section run to a new project and the server doesn't ever throw this error, and it works every time the bean is requested and every time the server is started.
Edit 1:
When I restart NetBeans and Clean and Build the project after it starts it says this:
Note: C:\Users\Administrator\Documents\NetBeansProjects\OIUSA_1\src\java\Beans\RTU\RTU_SmallView_Bean.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
This is the only java class it says this about, so here is the code I used for that class:
package Beans.RTU;
import Database.RTU.RTU_SmallView_Select;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import pojo.rtu.RTU_unit;
/**
*
* #author Administrator
*/
#Named(value = "rtu_SmallView_Bean")
#RequestScoped
public class RTU_SmallView_Bean {
public RTU_SmallView_Bean() {
try {
RTU_SmallView_Select selectData;
selectData = new RTU_SmallView_Select();
this.smallViewList = selectData.getData();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
List<RTU_unit> smallViewList = new ArrayList();
String unit_type;
int unit_number;
String rig_name;
String location_name;
public List<RTU_unit> getSmallViewList() {
return smallViewList;
}
public void setSmallViewList(List<RTU_unit> smallViewList) {
this.smallViewList = smallViewList;
}
public String getUnit_type() {
return unit_type;
}
public void setUnit_type(String unit_type) {
this.unit_type = unit_type;
}
public int getUnit_number() {
return unit_number;
}
public void setUnit_number(int unit_number) {
this.unit_number = unit_number;
}
public String getRig_name() {
return rig_name;
}
public void setRig_name(String rig_name) {
this.rig_name = rig_name;
}
public String getLocation_name() {
return location_name;
}
public void setLocation_name(String location_name) {
this.location_name = location_name;
}
}
My project structure is as follows:
Sources:
Beans.RTU.RTU_SmallView_Bean.java
Database.RTU.RTU_SmallView_Select.java
pojo.rtu.RTU_unit.java
Webpages:
rtu.rtu_smallview.xhtml
I am thinking it has something to do with the actual server, but I'm not sure where to start looking for this error. If you would like to see the actual code for the beans and what not, let me know and I'll edit the question with all the code. Thanks
Problem has been solved, the file RTU_Unit.java was in a folder called pojo.rtu. I deleted the folder, made it again with a new name pojo.rtus, refactored the file RTU_Unit.java for the new folder and the problem has gone away.
Im using #lgoncalves code to sign an XML with XADES4J EPES. But however jdeveloper don't find (SignerEPES) when I have the xades4j.jar on my classpath. I let you the image of my library and the code:
Project Library
private static void signBes(Document doc) throws XadesProfileResolutionException, XAdES4jException,
KeyStoreException {
//Document doc = getTestDocument();
Element elemToSign = doc.getDocumentElement();
SignaturePolicyInfoProvider policyInfoProvider = new SignaturePolicyInfoProvider()
{
#Override
public SignaturePolicyBase getSignaturePolicy()
{
return new SignaturePolicyIdentifierProperty(
new ObjectIdentifier("oid:/1.2.4.0.9.4.5", IdentifierType.OIDAsURI, "Policy description"),
new ByteArrayInputStream("Test policy input stream".getBytes()))
.withLocationUrl(""); //<- I really don't know what to put right here.
}
};
KeyingDataProvider kdp = new FileSystemKeyStoreKeyingDataProvider("pkcs12","C:/****/****.pfx",new FirstCertificateSelector(),new DirectPasswordProvider("****"),new DirectPasswordProvider("****"),true);
SignerEPES signer = (SignerEPES) new XadesEpesSigningProfile(kdp, policyInfoProvider).newSigner();
new Enveloped(signer).sign(elemToSign);
}
Link to the sample code on GitHub: https://github.com/luisgoncalves/xades4j/blob/master/src/test/java/xades4j/production/SignerEPESTest.java
EDIT:
I tried to force the import like (import xades4j.production.SignerEPES) but IDE says "Cannot be accessed from outside package" but really don't know what that means
SignerEPES is a package-private class, so application code won't be able to import it. The tests use it just to be sure that the proper type is being returned.
In your code you can just use XadesSigner as the type of your variable.
I am new to Sikuli and I wanted to
1. click windows button, and
2. type "Helloworld"
3. press Enter.
I have coded this and working Successfully in Sikuli IDE
click("1391583846712.png")
type("helloWorld")
wait(2)
type(Key.ENTER)
I tried to move this to Java ,
From the sikuli javadocs I have seen the following code, However it is not working in java sikuli-api-1.0.2 and latest version
import org.sikuli.script.*;
public class TestSikuli {
public static void main(String[] args) {
Screen s = new Screen();
try{
s.click("imgs/win-start.png", 0);
s.wait("imgs/spotlight-input.png");
s.type(null, "hello world\n", 0);
}
catch(FindFailed e){
e.printStackTrace();
}
}
}
It tells that Screen is an interface . Please tell me how to make it working in latest java sikuli-api. Please see that I am very new to Sikuli . Any suggestions will be highly appreciated. Also Please point me to the right sikuli java for begineers
new org.sikuli.api.DesktopScreenRegion() creates a ScreenRegion on the base full screen where you can click and seek your images
Your best bet to find how the new API is built is to look at the sources. There aren't a lot of classes to understand, fortunately.
The following Sikuli Java code should work:
import org.sikuli.script.*;
public class HelloWorld {
public static void main(String[] args){
Screen screen = new Screen();
try{
screen.click("D:\\Sikuli\\WinStartButton.png");
//"WinStartButton.png" must exist on the desired location you are using
//OR, instead of above line you can use the following:
screen.type(Key.WIN);
}
catch(FindFailed e){
e.getStackTrace();
}
screen.type("Hello World");
screen.type(Key.ENTER);
}
}
Try to use image locator in your code,
import org.sikuli.script.*;
import org.sikuli.basics.ImageLocator;
public class AuthLogin {
public static void main(String[] args) {
Screen s = new Screen();
ImageLocator.setBundlePath("path to img directory");
try{
s.click("win-start.png", 0);
s.wait("spotlight-input.png");
s.type(null, "hello world\n", 0);
}
catch(FindFailed e){
e.printStackTrace();
}
}
I think you should not use the absolute image path directly in the code.
I would create a class which contains the absolute paths as static constants.
Example :
instead of :
screen.click( "D:\\Sikuli\\WinStartButton.png");
you can do it like this :
public static final String IMAGE = "D:\\Sikuli\\WinStartButton.png";
screen.click(IMAGE);
Use Keydown and Keyup method for Enter key pressed
I have tried to organize the entire code. please let me know if its working.
Screen sikuli = new Screen();
String message = "hello world";
Pattern imgLocator = "";
if(sikuli.exists(imgLocator)!=null) {
sikuli.find(imgLocator);
sikuli.click(imgLocator);
sikuli.wait(2);
sikuli.type(imgLocator, message);
}
sikuli.keyDown(Key.ENTER);
sikuli.keyUp(Key.ENTER);
I am programming in Java using Maven/Eclipse.
I am able to run HtmlUnit fine when running from a Unit Test. (test/)
However when I try to use the same code in the src/ folder, I receive java.lang.NoClassDefFoundError messages. The only way I have been able to resolve them is to go and manually add all the jars to the build path. But this doesn't make sense to me as the jar file shows up in my Maven Dependencies.
pom.xml (there are more dependencies in the actual pom file)
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.9</version>
<scope>test</scope>
</dependency>
The Sample block of HtmlUnit code
WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage(url);
System.out.println("Executing Pages JavaScript for " + config.executeJavaScriptTime + " seconds..."); webClient.waitForBackgroundJavaScript(config.executeJavaScriptTime);
dom = cleaner.clean(page.asXml());
html = cleaner.getInnerHtml(dom);
webClient.closeAllWindows();
Any Ideas? Thanks.
The test scope which is applied to your dependency means that it is not available on the compile classpath. This way, the code you ship does not depend on test code. A more complete explanation can be found here. If the project you are building is intended to be only tests, you should remove the scope tag to let it take the default scope, compile. But in general it is correct that your shipping code, built from src, shouldn't depend on JUnit, a testing library.
As of now the only working hack I have found is to create a method annotated as #Test and run the crawler as a JUnit Test.
i.e.
public class Crawler() {
#Test
public void runAsTest() {
Crawler crawler = new Crawler();
String[] urls = new String[]{
"http://www.url.com/1",
"http://www.url.com/2",
"http://www.url.com/3"
};
crawler.crawl(urls);
try {
Thread.sleep(1000 * 60 * 15); // without this the unit test exits too early
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// the rest of the class definition
}
I would like to be able to run this from a standard main method, i.e.
public class Crawler() {
public static void main(String[] args) {
Crawler crawler = new Crawler();
String[] urls = new String[]{
"http://www.url.com/1",
"http://www.url.com/2",
"http://www.url.com/3"
};
crawler.crawl(urls);
try {
Thread.sleep(1000 * 60 * 15); // without this the unit test exits too early
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// the rest of the class definition
}
Knowing nothing of web services, I'm just trying to call some "isAlive" service that is described by a wsdl.
This seems to me like something that should take no more than 2-5 lines of code but I can't seem to find anything but huge long examples involving 3rd party packages etc.
Anyone has any ideas? If it is always suppose to be long maybe a good explanation as to why it has to be so complicated will also be appreciated.
I'm using Eclipse and the wsdl is SOAP.
JDK 6 comes with jax-ws, everything you need to develop a client for a web service.
I'm unable to find some simple enough examples to post , but start at https://jax-ws.dev.java.net/
Edit: here's a simple example - a client for this web service: http://xmethods.com/ve2/ViewListing.po?key=427565
C:\temp> md generated
C:\temp>"c:\Program Files\Java\jdk1.6.0_17"\bin\wsimport -keep -d generated http://www50.brinkster.com/vbfacileinpt/np.asmx?wsdl
Create PrimeClient.java which look like:
import javax.xml.ws.WebServiceRef;
import com.microsoft.webservices.*;
//the above namespace is from the generated code from the wsdl.
public class PrimeClient {
//Cant get this to work.. #WebServiceRef(wsdlLocation="http://www50.brinkster.com/vbfacileinpt/np.asmx?wsdl")
static PrimeNumbers service;
public static void main(String[] args) {
try {
service = new PrimeNumbers();
PrimeClient client = new PrimeClient();
client.doTest(args);
} catch(Exception e) {
e.printStackTrace();
}
}
public void doTest(String[] args) {
try {
System.out.println("Retrieving the port from the following service: " + service);
PrimeNumbersSoap pm = service.getPrimeNumbersSoap();
System.out.println("Invoking the getPrimeNumbersSoap operation ");
System.out.println(pm.getPrimeNumbers(100));
} catch(Exception e) {
e.printStackTrace();
}
}
}
Compile and run:
C:\temp>"c:\Program Files\Java\jdk1.6.0_17"\bin\javac -cp generated PrimeClient.java
C:\temp>"c:\Program Files\Java\jdk1.6.0_17"\bin\java -cp .;generated PrimeClient
Retrieving the port from the following service: com.microsoft.webservices.PrimeN
umbers#19b5393
Invoking the getPrimeNumbersSoap operation
1,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97
There are plugins for IDE's which generate the needed code to consume a web service for you.
After the plugin generates you the base methods you simply call a web service like that:
TransportServiceSoap service = new TransportServiceLocator().getTransportServiceSoap();
service.getCities();
Have a look at http://urbas.tk/index.php/2009/02/20/eclipse-plug-in-as-a-web-service-client/
There are three ways to write a web service client
Dynamic proxy
Dynamic invocation interface (DII)
Application client
Example for Dynamic Proxy Client
import java.net.URL;
import javax.xml.rpc.Service;
import javax.xml.rpc.JAXRPCException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceFactory;
import dynamicproxy.HelloIF;
public class HelloClient {
public static void main(String[] args) {
try {
String UrlString = "Your WSDL URL"; //
String nameSpaceUri = "urn:Foo";
String serviceName = "MyHelloService";
String portName = "HelloIFPort";
System.out.println("UrlString = " + UrlString);
URL helloWsdlUrl = new URL(UrlString);
ServiceFactory serviceFactory =
ServiceFactory.newInstance();
Service helloService =
serviceFactory.createService(helloWsdlUrl,
new QName(nameSpaceUri, serviceName));
dynamicproxy.HelloIF myProxy =
(dynamicproxy.HelloIF)
helloService.getPort(
new QName(nameSpaceUri, portName),
dynamicproxy.HelloIF.class);
System.out.println(myProxy.sayHello("Buzz"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
I hope , this would solve your question.
The easiest I've found so far to use is the Idea IntelliJ wizard which - using Metro libraries - generate a very small code snippet which works fine with Java 6.