How to use Enum Availability class of org.openqa.selenium.grid.data package

Best Selenium code snippet using org.openqa.selenium.grid.data.Enum Availability

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18package org.openqa.selenium.grid.data;
19
20public enum Availability {
21  UP,
22  DRAINING,
23  DOWN
24}
25
Full Screen
copy
1patient_type | admission_type | inpatient_or_outpatient
2-------------------------------------------------------
3'A'          | 'O'            | 'Outpatient'
4'B'          | NULL           | 'Inpatient'
5
Full Screen
copy
1def sendToOutPatient = { ... };
2
3def sendToInPatient = { ... };
4
5def patientRule = { PATIENT_TYPE ->
6    {'A': sendToOutPatient,
7     'B': sendToInPatient}.get(PATIENT_TYPE)
8}
9
10static main(){
11    (patientRule('A'))()
12}
13
Full Screen
copy
1GroovyShell shell = new GroovyShell(binding);
2binding.setVariable("foo", "World");
3System.out.println(shell.evaluate("println 'Hello ${foo}!';));
4
Full Screen
copy
1public abstract class AbstractRule{
2  public boolean canHandle(JSONObject rawRuleData){
3    return StringUtils.equals(getClass().getSimpleName(), rawRuleData.getString("ruleClassName"));
4  }
5  public abstract void parseRawRuleDataIntoThis(JSONObject rawRuleData); //throw some validation exception
6  public abstract RuleResult execute();
7}
8
Full Screen
copy
1public class InOutPatientRule extends AbstractRule{
2  private String patientType;
3  private String admissionType;
4
5  public void parseRawRuleDataIntoThis(JSONObject rawRuleData){
6    this.patientType = rawRuleData.getString("patientType");
7    this.admissionType= rawRuleData.getString("admissionType");
8  }
9  public RuleResultInOutPatientType execute(){
10    if(StringUtils.equals("A",this.patientType) && StringUtils.equals("O",this.admissionType)){
11      return //OUTPATIENT
12    }
13    return //INPATIENT
14  }
15}
16
Full Screen
copy
1import java.util.Map;
2
3public interface Expression
4{
5    public boolean interpret(final Map<String, ?> bindings);
6}
7
Full Screen
copy
1import java.util.Map;
2
3public class Variable implements Expression
4{
5    private String name;
6
7    public Variable(String name)
8    {
9        this.name = name;
10    }
11
12    public String getName()
13    {
14        return this.name;
15    }
16
17    @Override
18    public boolean interpret(Map<String, ?> bindings)
19    {
20        return true;
21    }
22}
23
Full Screen
copy
1import java.util.Map;
2
3public class BaseType<T> implements Expression
4{
5    public T value;
6    public Class<T> type;
7
8    public BaseType(T value, Class<T> type)
9    {
10        this.value = value;
11        this.type = type;
12    }
13
14    public T getValue()
15    {
16        return this.value;
17    }
18
19    public Class<T> getType()
20    {
21        return this.type;
22    }
23
24    @Override
25    public boolean interpret(Map<String, ?> bindings)
26    {
27        return true;
28    }
29
30    public static BaseType<?> getBaseType(String string)
31    {
32        if (string == null)
33            throw new IllegalArgumentException("The provided string must not be null");
34
35        if ("true".equals(string) || "false".equals(string))
36            return new BaseType<>(Boolean.getBoolean(string), Boolean.class);
37        else if (string.startsWith("'"))
38            return new BaseType<>(string, String.class);
39        else if (string.contains("."))
40            return new BaseType<>(Float.parseFloat(string), Float.class);
41        else
42            return new BaseType<>(Integer.parseInt(string), Integer.class);
43    }
44}
45
Full Screen
copy
1import java.util.Stack;
2
3public abstract class Operation implements Expression
4{
5    protected String symbol;
6
7    protected Expression leftOperand = null;
8    protected Expression rightOperand = null;
9
10    public Operation(String symbol)
11    {
12        this.symbol = symbol;
13    }
14
15    public abstract Operation copy();
16
17    public String getSymbol()
18    {
19        return this.symbol;
20    }
21
22    public abstract int parse(final String[] tokens, final int pos, final Stack<Expression> stack);
23
24    protected Integer findNextExpression(String[] tokens, int pos, Stack<Expression> stack)
25    {
26        Operations operations = Operations.INSTANCE;
27
28        for (int i = pos; i < tokens.length; i++)
29        {
30            Operation op = operations.getOperation(tokens[i]);
31            if (op != null)
32            {
33                op = op.copy();
34                // we found an operation
35                i = op.parse(tokens, i, stack);
36
37                return i;
38            }
39        }
40        return null;
41     }
42}
43
Full Screen
copy
1import java.util.Map;
2import java.util.Stack;
3
4public class And extends Operation
5{    
6    public And()
7    {
8        super("AND");
9    }
10
11    public And copy()
12    {
13        return new And();
14    }
15
16    @Override
17    public int parse(String[] tokens, int pos, Stack<Expression> stack)
18    {
19        Expression left = stack.pop();
20        int i = findNextExpression(tokens, pos+1, stack);
21        Expression right = stack.pop();
22
23        this.leftOperand = left;
24        this.rightOperand = right;
25
26        stack.push(this);
27
28        return i;
29    }
30
31    @Override
32    public boolean interpret(Map<String, ?> bindings)
33    {
34        return leftOperand.interpret(bindings) && rightOperand.interpret(bindings);
35    }
36}
37
Full Screen
copy
1import java.util.Map;
2import java.util.Stack;
3
4public class Not extends Operation
5{    
6    public Not()
7    {
8        super("NOT");
9    }
10
11    public Not copy()
12    {
13        return new Not();
14    }
15
16    @Override
17    public int parse(String[] tokens, int pos, Stack<Expression> stack)
18    {
19        int i = findNextExpression(tokens, pos+1, stack);
20        Expression right = stack.pop();
21
22        this.rightOperand = right;
23        stack.push(this);
24
25        return i;
26    }
27
28    @Override
29    public boolean interpret(final Map<String, ?> bindings)
30    {
31        return !this.rightOperand.interpret(bindings);
32    }    
33}
34
Full Screen
copy
1import java.util.Map;
2import java.util.Stack;
3
4public class Equals extends Operation
5{      
6    public Equals()
7    {
8        super("=");
9    }
10
11    @Override
12    public Equals copy()
13    {
14        return new Equals();
15    }
16
17    @Override
18    public int parse(final String[] tokens, int pos, Stack<Expression> stack)
19    {
20        if (pos-1 >= 0 && tokens.length >= pos+1)
21        {
22            String var = tokens[pos-1];
23
24            this.leftOperand = new Variable(var);
25            this.rightOperand = BaseType.getBaseType(tokens[pos+1]);
26            stack.push(this);
27
28            return pos+1;
29        }
30        throw new IllegalArgumentException("Cannot assign value to variable");
31    }
32
33    @Override
34    public boolean interpret(Map<String, ?> bindings)
35    {
36        Variable v = (Variable)this.leftOperand;
37        Object obj = bindings.get(v.getName());
38        if (obj == null)
39            return false;
40
41        BaseType<?> type = (BaseType<?>)this.rightOperand;
42        if (type.getType().equals(obj.getClass()))
43        {
44            if (type.getValue().equals(obj))
45                return true;
46        }
47        return false;
48    }
49}
50
Full Screen
copy
1import java.util.Stack;
2
3public class ExpressionParser
4{
5    private static final Operations operations = Operations.INSTANCE;
6
7    public static Expression fromString(String expr)
8    {
9        Stack<Expression> stack = new Stack<>();
10
11        String[] tokens = expr.split("\\s");
12        for (int i=0; i < tokens.length-1; i++)
13        {
14            Operation op = operations.getOperation(tokens[i]);
15            if ( op != null )
16            {
17                // create a new instance
18                op = op.copy();
19                i = op.parse(tokens, i, stack);
20            }
21        }
22
23        return stack.pop();
24    }
25}
26
Full Screen
copy
1import java.util.HashMap;
2import java.util.Map;
3import java.util.Set;
4
5public enum Operations
6{
7    /** Application of the Singleton pattern using enum **/
8    INSTANCE;
9
10    private final Map<String, Operation> operations = new HashMap<>();
11
12    public void registerOperation(Operation op, String symbol)
13    {
14        if (!operations.containsKey(symbol))
15            operations.put(symbol, op);
16    }
17
18    public void registerOperation(Operation op)
19    {
20        if (!operations.containsKey(op.getSymbol()))
21            operations.put(op.getSymbol(), op);
22    }
23
24    public Operation getOperation(String symbol)
25    {
26        return this.operations.get(symbol);
27    }
28
29    public Set<String> getDefinedSymbols()
30    {
31        return this.operations.keySet();
32    }
33}
34
Full Screen
copy
1import java.util.ArrayList;
2import java.util.List;
3import java.util.Map;
4
5public class Rule
6{
7    private List<Expression> expressions;
8    private ActionDispatcher dispatcher;
9
10    public static class Builder
11    {
12        private List<Expression> expressions = new ArrayList<>();
13        private ActionDispatcher dispatcher = new NullActionDispatcher();
14
15        public Builder withExpression(Expression expr)
16        {
17            expressions.add(expr);
18            return this;
19        }
20
21        public Builder withDispatcher(ActionDispatcher dispatcher)
22        {
23            this.dispatcher = dispatcher;
24            return this;
25        }
26
27        public Rule build()
28        {
29            return new Rule(expressions, dispatcher);
30        }
31    }
32
33    private Rule(List<Expression> expressions, ActionDispatcher dispatcher)
34    {
35        this.expressions = expressions;
36        this.dispatcher = dispatcher;
37    }
38
39    public boolean eval(Map<String, ?> bindings)
40    {
41        boolean eval = false;
42        for (Expression expression : expressions)
43        {
44            eval = expression.interpret(bindings);
45            if (eval)
46                dispatcher.fire();
47        }
48        return eval;
49    }
50}
51
Full Screen
copy
1public interface ActionDispatcher
2{
3    public void fire();
4}
5
Full Screen
copy
1public class InPatientDispatcher implements ActionDispatcher
2{
3    @Override
4    public void fire()
5    {
6        // send patient to in_patient
7        System.out.println("Send patient to IN");
8    }
9}
10
Full Screen
copy
1import java.util.HashMap;
2import java.util.Map;
3
4public class Main 
5{
6    public static void main( String[] args )
7    {
8        // create a singleton container for operations
9        Operations operations = Operations.INSTANCE;
10
11        // register new operations with the previously created container
12        operations.registerOperation(new And());
13        operations.registerOperation(new Equals());
14        operations.registerOperation(new Not());
15
16        // defines the triggers when a rule should fire
17        Expression ex3 = ExpressionParser.fromString("PATIENT_TYPE = 'A' AND NOT ADMISSION_TYPE = 'O'");
18        Expression ex1 = ExpressionParser.fromString("PATIENT_TYPE = 'A' AND ADMISSION_TYPE = 'O'");
19        Expression ex2 = ExpressionParser.fromString("PATIENT_TYPE = 'B'");
20
21        // define the possible actions for rules that fire
22        ActionDispatcher inPatient = new InPatientDispatcher();
23        ActionDispatcher outPatient = new OutPatientDispatcher();
24
25        // create the rules and link them to the accoridng expression and action
26        Rule rule1 = new Rule.Builder()
27                            .withExpression(ex1)
28                            .withDispatcher(outPatient)
29                            .build();
30
31        Rule rule2 = new Rule.Builder()
32                            .withExpression(ex2)
33                            .withExpression(ex3)
34                            .withDispatcher(inPatient)
35                            .build();
36
37        // add all rules to a single container
38        Rules rules = new Rules();
39        rules.addRule(rule1);
40        rules.addRule(rule2);
41
42        // for test purpose define a variable binding ...
43        Map<String, String> bindings = new HashMap<>();
44        bindings.put("PATIENT_TYPE", "'A'");
45        bindings.put("ADMISSION_TYPE", "'O'");
46        // ... and evaluate the defined rules with the specified bindings
47        boolean triggered = rules.eval(bindings);
48        System.out.println("Action triggered: "+triggered);
49    }
50}
51
Full Screen
copy
1class A 
2{
3public void foo() {code}
4}
5
6class B:A
7{
8public void foo() {code}
9}    
10
11void SomeMethod()
12{
13A a = new B(); // variable's type is declared as A, but assigned to an object of B.
14a.foo();
15}
16
Full Screen
copy
1public enum Country{
2
3    ANDORRA(new Locale("AD")),
4    AFGHANISTAN(new Locale("AF")),
5    ANTIGUA_AND_BARBUDA(new Locale("AG")),
6    ANGUILLA(new Locale("AI")),
7    //etc
8    ZAMBIA(new Locale("ZM")),
9    ZIMBABWE(new Locale("ZW"));
10
11    private Locale locale;
12
13    private Country(Locale locale){
14        this.locale = locale;
15    }
16
17    public Locale getLocale(){
18        return locale;
19    }
20
Full Screen
copy
1#!/usr/bin/python
2f = open("data.txt", 'r')
3data = []
4cc = {}
5
6for l in f:
7    t = l.split('\t')
8    cc = { 'code': str(t[0]).strip(), 
9           'name': str(t[1]).strip()
10    }
11    data.append(cc)
12f.close()
13
14for c in data:
15    print """
16/**
17 * Defines the <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2">ISO_3166-1_alpha-2</a> 
18 * for <b><i>%(name)s</i></b>.
19 * <p>
20 * This constant holds the value of <b>{@value}</b>.
21 *
22 * @since 1.0
23 *
24 */
25 public static final String %(code)s = \"%(code)s\";""" % c
26
Full Screen
copy
1/**
2 * Holds <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2">ISO_3166-1_alpha-2</a>
3 * constant values for all countries. 
4 * 
5 * @since 1.0
6 * 
7 * </p>
8 */
9public class CountryCode {
10
11    /**
12     * Constructor defined as <code>private</code> purposefully to ensure this 
13     * class is only used to access its static properties and/or methods.  
14     */
15    private CountryCode() { }
16
17    /**
18     * Defines the <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2">ISO_3166-1_alpha-2</a> 
19     * for <b><i>Andorra</i></b>.
20     * <p>
21     * This constant holds the value of <b>{@value}</b>.
22     *
23     * @since 1.0
24     *
25     */
26     public static final String AD = "AD";
27
28         //
29         // and the list goes on! ...
30         //
31}
32
Full Screen
copy
1 /**
2  * This is the code used to generate the enum content
3  */
4 public static void main(String[] args) {
5  String[] codes = java.util.Locale.getISOLanguages();
6  for (String isoCode: codes) {
7   Locale locale = new Locale(isoCode);
8   System.out.println(isoCode.toUpperCase() + "(\"" + locale.getDisplayLanguage(locale) + "\"),");
9  }
10 }
11
Full Screen
copy
1private HashMap<String, String> countries = new HashMap<String, String>();
2String[] countryCodes = Locale.getISOCountries();
3
4for (String cc : countryCodes) {
5    // country name , country code map
6    countries.put(new Locale("", cc).getDisplayCountry(), cc.toUpperCase());
7}
8
Full Screen
copy
1package countryenum;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.List;
6import java.util.Locale;
7
8public class CountryEnumGenerator {
9    public static void main(String[] args) {
10        String[] countryCodes = Locale.getISOCountries();
11        List<Country> list = new ArrayList<Country>(countryCodes.length);
12
13        for (String cc : countryCodes) {
14            list.add(new Country(cc.toUpperCase(), new Locale("", cc).getDisplayCountry()));
15        }
16
17        Collections.sort(list);
18
19        for (Country c : list) {
20            System.out.println("/**" + c.getName() + "*/");
21            System.out.println(c.getCode() + "(\"" + c.getName() + "\"),");
22        }
23
24    }
25}
26
27class Country implements Comparable<Country> {
28    private String code;
29    private String name;
30
31    public Country(String code, String name) {
32        super();
33        this.code = code;
34        this.name = name;
35    }
36
37    public String getCode() {
38        return code;
39    }
40
41
42    public void setCode(String code) {
43        this.code = code;
44    }
45
46
47    public String getName() {
48        return name;
49    }
50
51
52    public void setName(String name) {
53        this.name = name;
54    }
55
56
57    @Override
58    public int compareTo(Country o) {
59        return this.name.compareTo(o.name);
60    }
61}
62
Full Screen
copy
1String[] countryCodes = Locale.getISOCountries();
2
Full Screen
copy
1CountryCode cc = CountryCode.getByCode("JP");
2
3System.out.println("Country name = " + cc.getName());                // "Japan"
4System.out.println("ISO 3166-1 alpha-2 code = " + cc.getAlpha2());   // "JP"
5System.out.println("ISO 3166-1 alpha-3 code = " + cc.getAlpha3());   // "JPN"
6System.out.println("ISO 3166-1 numeric code = " + cc.getNumeric());  // 392
7
Full Screen
copy
1<dependency>
2  <groupId>com.neovisionaries</groupId>
3  <artifactId>nv-i18n</artifactId>
4  <version>1.29</version>
5</dependency>
6
Full Screen
copy
1dependencies {
2  compile 'com.neovisionaries:nv-i18n:1.29'
3}
4
Full Screen
copy
1Bundle-SymbolicName: com.neovisionaries.i18n
2Export-Package: com.neovisionaries.i18n;version="1.28.0"
3
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in Enum-Availability

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)