Is there any API available to get how many slots of specific browser available in selenium grid? - linux

I would like to get the number of tests running with a specific browser in the selenium grid.
I have looked at the existing API, where i can get the slotcount which is the sum of all the available slots which includes all the browsers.
ex: curl -X GET http://localhost:4444/grid/api/hub/ -d '{"configuration":["slotCounts"]}'
Output will be: {"success":true,"slotCounts":{"free":178,"total":196}}
Is there any API available to get how many slots of chrome browser(say) available?
Other options come to my mind is to parse the existing API
curl -X GET http://localhost:4444/grid/console
which return the full stack, where i need to parse the html structure, which is like
<img src='/grid/resources/org/openqa/grid/images/chrome.png' width='16' height='16' class='busy' title='POST - /session/8802ebae-10cb-480d-bbbd-5e7edd7ee7b2/execute executed.' />

No. Currently there's no such API available out there in the Selenium Grid that can do this for you.
You would need to build a custom servlet which when invoked can extract and provide this information for you.
Your Hub servlet could look like this:
import org.openqa.grid.internal.ProxySet;
import org.openqa.grid.internal.Registry;
import org.openqa.grid.internal.RemoteProxy;
import org.openqa.grid.web.servlet.RegistryBasedServlet;
import org.openqa.selenium.remote.CapabilityType;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class SimpleServlet extends RegistryBasedServlet {
public SimpleServlet(Registry registry) {
super(registry);
}
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ProxySet proxySet = getRegistry().getAllProxies();
Iterator<RemoteProxy> iterator = proxySet.iterator();
Map<String, List<String>> returnValue = new HashMap<>();
while (iterator.hasNext()) {
RemoteProxy each = iterator.next();
each.getTestSlots().forEach(slot -> {
String browser = (String) slot.getCapabilities().get(CapabilityType.BROWSER_NAME);
String machineIp = each.getRemoteHost().getHost();
List<String> machines = returnValue.get(browser);
if (machines == null) {
machines = new ArrayList<>();
}
machines.add(machineIp);
returnValue.put(browser, machines);
});
}
//Write logic to have the Map returned back as perhaps a JSON payload
}
}
You can refer to the Selenium documentation here to learn how to inject servlets to the Hub or the node.

Related

Groovy list files/directory in an http connection

I'm using a Java library to list all files/directory in a remote http URL:
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class Sample {
public static void main(String[] args) throws IOException {
Document doc = Jsoup.connect("http://howto.unixdev.net").get();
for (Element file : doc.select("td.right td a")) {
System.out.println(file.attr("href"));
}
}
}
I'd need to do the equivalent thing with a Groovy script (as part of a Jenkins job) but I couldn't find any example of it, except for file system listing. Any help?
Thanks!

How to print RuntimeVisibleAnnotations in java ASM

I am new to ASM. I have a class file in which I have runtime visible annotations for methods. I want to parse this class file and select the annotation according to specific criteria. I looked into the documentation of ASM and tried with the visibleAnnotation. I can't seem to print the list of annotations of method which I can see in my class files.
My code is as
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.ClassReader;
public class ByteCodeParser {
public static void main(String[] args) throws Exception{
InputStream in=new FileInputStream("sample.class");
ClassReader cr=new ClassReader(in);
ClassNode classNode=new ClassNode();
//ClassNode is a ClassVisitor
cr.accept(classNode, 0);
//
Iterator<MethodNode> i = classNode.methods.iterator();
while(i.hasNext()){
MethodNode mn = i.next();
System.out.println(mn.name+ "" + mn.desc);
System.out.println(mn.visibleAnnotations);
}
}
}
The output is:
<clinit>()V
null
<init>()V
null
MyRandomFunction1()V
[org.objectweb.asm.tree.AnnotationNode#5674cd4d]
MyRandomFunction2()V
[org.objectweb.asm.tree.AnnotationNode#63961c42]
My RandomFunction 1 & 2 has annotations but I can't seem to understand [org.objectweb.asm.tree.AnnotationNode#5674cd4d].
I solved this issue myself, I had to iterate over the annotations which I didn't realize initally.
if (mn.visibleAnnotations != null) {
Iterator<AnnotationNode>j=mn.visibleAnnotations.iterator();
while (j.hasNext()) {
AnnotationNode an=j.next();
System.out.println(an.values);
}
}

How can I make Selenium tests inside my JSF project?

I have quite big JSF 1.2 project and I want to write some integration tests to it.
The perfect situation will be, when I can run these tests from my project and it opens my browser and makes all the actions (with Selenium), which are written in my test cases. Ofc opening browser is not required when It will run these tests anyway :)
I've tried a few possibilities, anyway I still can't attach any selenium library to my project and I realized that I just dont know where to start - can you give me some direction?
might help you ,
you can write you test logic inside test method
package com.test;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.fail;
public class test1 {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
driver = new InternetExplorerDriver();
driver = new ChromeDriver();
baseUrl = "http://www.google.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Ignore
#Test
public void test1() throws Exception {
// your test code
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
you just need call test1 class which you want to test it .
it will be automatically working on it .

how doI bind a textfield with form in j2me using lwuit?

I have a problem in below code, this frmChinese.append(txtField); is not working for me.
What is the correct way to bind this text field with my form? The header files and libraries that I have used have also been mentioned.
package com.lbs;
import com.lbs.MidletSplashScreen;
import com.sun.lwuit.*;
import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.Border;
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.TextField;
public class Chinese extends Form implements ActionListener {
Form frmChinese = null;
Command cmdExit = null;
TextField txtField = null;
Chinese() {
frmChinese = this;
frmChinese.setTitle("Chinese");
frmChinese.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
this.getStyle().setBgColor(0xFF5240);
ShowUi();
cmdExit = new Command("Exit");
Command cmdBack = new Command("Back"); // new command with name Back
frmChinese.addCommand(cmdExit);// add command in Form
frmChinese.addCommand(cmdBack);// add command in Form
frmChinese.setBackCommand(cmdBack); // setting back command
frmChinese.addCommandListener(this); // register action listener in form
}
private void ShowUi() {
txtField = new TextField("","Search", 20, TextField.ANY);
frmChinese.append(txtField);
}
What you need to use is te addComponent method from the Form Class. In lwuit the basic interface elements are Components. Adding this Components to your Form's Layout, you can built some cool designs.
Take a look here:
LWUIT Layouts

Is there an easier way to tell HTTPBuilder to ignore an invalid cert?

Per the docs, you can go through a rather clunky process of export a cert from a browser manually and getting it recognized locally. Is there anything similar to curl's --insecure switch to make this practical?
Good news everyone! :-)
Just found out that new version (0.7.1) of HttpBuilder introduces method:
ignoreSSLIssues()
This solves all problems regarding invalid SSL certificates (of course you have to be aware that it also decreases security).
More information about this method: https://github.com/jgritman/httpbuilder/wiki/SSL (section at the bottom)
Found a way that non involve import of certificates or httpbuilder hacks
//== HTTPBUILDER IMPORTS
#Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2' )
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
//== END HTTPBUILDER IMPORTS
import javax.net.ssl.X509TrustManager
import javax.net.ssl.SSLContext
import java.security.cert.X509Certificate
import javax.net.ssl.TrustManager
import java.security.SecureRandom
import org.apache.http.conn.ssl.SSLSocketFactory
import org.apache.http.conn.scheme.Scheme
import org.apache.http.conn.scheme.SchemeRegistry
def http = new HTTPBuilder( "https://your_unsecure_certificate_host" )
//=== SSL UNSECURE CERTIFICATE ===
def sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, [ new X509TrustManager() {public X509Certificate[]
getAcceptedIssuers() {null }
public void checkClientTrusted(X509Certificate[] certs, String authType) { }
public void checkServerTrusted(X509Certificate[] certs, String authType) { }
} ] as TrustManager[], new SecureRandom())
def sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
def httpsScheme = new Scheme("https", sf, 443)
http.client.connectionManager.schemeRegistry.register( httpsScheme )
//================================
//do your http call with the http object
http.request( ....

Resources