How to use ById class of org.openqa.selenium.grid.node.locators package

Best Selenium code snippet using org.openqa.selenium.grid.node.locators.ById

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.node.locators;
19
20import com.google.auto.service.AutoService;
21
22import org.openqa.selenium.By;
23import org.openqa.selenium.internal.Require;
24import org.openqa.selenium.remote.locators.CustomLocator;
25
26/**
27 * A class implementing {link @CustomLocator} and to be used as a fallback locator on the server
28 * side.
29 */
30
31@AutoService(CustomLocator.class)
32public class ById implements CustomLocator {
33  @Override
34  public String getLocatorName() {
35    return "id";
36  }
37
38  @Override
39  public By createBy(Object usingParameter) {
40    Require.argument("Locator value", usingParameter).instanceOf(String.class);
41    return By.id((String) usingParameter);
42  }
43}
44
Full Screen
copy
1public static Optional<EnumStatus> fromId(Long id) {
2    for (EnumStatus e: values()) {
3        if (e.id.equals(id)) {
4            return Optional.of(e);
5        }
6    }
7    return Optional.empty();
8}
9
Full Screen
copy
1public static Optional<EnumStatus> fromId(Long id) {
2    return Stream.of(values())
3            .filter(e -> e.id.equals(id))
4            .findFirst();
5}
6
Full Screen
copy
1private static final Map<String, Operation> stringToEnum =
2    Stream.of(values()).collect(
3        toMap(Object::toString, e -> e));
4
5public static Optional<Operation> fromString(String symbol) {
6    return Optional.ofNullable(stringToEnum.get(symbol));
7}
8
Full Screen
copy
1public interface Identifiable<T extends Number> {
2
3    @Nonnull
4    T getId();
5}
6
7public final class GettableById<K extends Number, V extends Enum<V> & Identifiable<K>> {
8
9    @Nonnull
10    private final Map<K, V> idToValue;
11
12
13    public GettableById(@Nonnull V[] values) {
14        this.idToValue = Arrays.stream(values)
15                .collect(Collectors.toUnmodifiableMap(Identifiable::getId, v -> v));
16    }
17
18    @Nonnull
19    public V getById(@Nonnull K id) {
20        var value = idToValue.get(id);
21        if (value != null) {
22            return value;
23        }
24        throw new NullPointerException("Cannot get value by id: %s".formatted(id));
25    }
26}
27
28public enum DataType implements Identifiable<Short> {
29
30    ANNUAL((short) 1), QUARTERLY((short) 2);
31
32    private static final GettableById<Short, DataType> companion = new GettableById<>(values());
33
34    @Nonnull
35    private final Short id;
36
37
38    public static DataType getById(Short id) {
39        return companion.getById(id);
40    }
41
42    DataType(@Nonnull Short id) {
43        this.id = id;
44    }
45
46    @Nonnull
47    @Override
48    public Short getId() {
49        return id;
50    }
51}
52
Full Screen
copy
1/**
2 * Contract that will allow Types with id to have generic implementation.
3 */
4public interface IdentifierType<T> {
5  T getId();
6}
7
Full Screen
copy
1public enum EntityType implements IdentifierType<Integer> {
2  ENTITY1(1, "ONE), ENTITY2(2, "TWO");
3
4  private Integer id;
5  private String name;
6
7  private EntityType(int id, String name) {
8    this.id = id;
9    this.name = name;
10  }
11
12  public static EntityType valueOf(Integer id) {
13    return EnumHelper.INSTANCE.valueOf(id, EntityType.values());
14  }
15
16  @Override
17  public Integer getId() {
18    return id;
19  }
20}
21
Full Screen
copy
1public enum EnumHelper {
2  INSTANCE;
3
4  /**
5   * This will return {@link Enum} constant out of provided {@link Enum} values with the specified id.
6   * @param id the id of the constant to return.
7   * @param values the {@link Enum} constants of specified type.
8   * @return the {@link Enum} constant.
9   */
10  public <T extends IdentifierType<S>, S> T valueOf(S id, T[] values) {
11    if (!values[0].getClass().isEnum()) {
12        throw new IllegalArgumentException("Values provided to scan is not an Enum");
13    }
14
15    T type = null;
16
17    for (int i = 0; i < values.length && type == null; i++) {
18        if (values[i].getId().equals(id)) {
19            type = values[i];
20        }
21    }
22
23    return type;
24  }
25}
26
Full Screen
copy
1public static EnumStatus fromId(Long id) {
2    int index = (id - 40L) / 20L;
3    return values()[index];
4}
5
Full Screen
copy
1for (EnumStatus  enumStatus : EnumStatus.values()) {
2   if (..) {..}
3}
4
Full Screen
copy
1 public static ArrayList<EnumStatus> getEnumStatusById(ArrayList<Long> idList) {
2    ArrayList<EnumStatus> listById = new ArrayList();
3    for(EnumStatus es: EnumStatus.values()) {
4        if( idList.contains(es.getId())) {
5            listById.add(es);
6        }
7    }
8    return listById;
9}
10
Full Screen
copy
1public static EnumStatus getById(long id)
2{
3  for (EnumStatus e : EnumStatus.values())
4  {
5    if (id == e.getId()) return e;
6  } 
7  throw new IllegalArgumentException("oh no");
8}
9
Full Screen
copy
1public static String fromId(long id) {
2        for (EnumStatus es : EnumStatus.values()) {
3            if (es.id.equals(id)) {
4                return es.getName();
5            }
6        }
7        throw new IllegalArgumentException();
8}
9
Full Screen
copy
1public enum EnumStatus {
2
3    PASSED(40L, "Has Passed"),
4    AVERAGE(60L, "Has Average Marks"),
5    GOOD(80L, "Has Good Marks");
6
7    private static final Map<Long, EnumStatus> byId = new HashMap<Long, EnumStatus>();
8    static {
9        for (EnumStatus e : EnumStatus.values()) {
10            if (byId.put(e.getId(), e) != null) {
11                throw new IllegalArgumentException("duplicate id: " + e.getId());
12            }
13        }
14    }
15
16    public static EnumStatus getById(Long id) {
17        return byId.get(id);
18    }
19
20    // original code follows
21
22    private java.lang.String name;
23
24    private java.lang.Long id;
25
26    EnumStatus(Long id, java.lang.String name) {
27        this.name = name;
28        this.id = id;
29    }
30
31    public java.lang.String getName() {
32        return name;
33    }
34
35    public java.lang.Long getId() {
36        return id;
37    }
38
39}
40
Full Screen
copy
1public static EnumStatus getById(Long id) {
2    for(EnumStatus e : values()) {
3        if(e.id.equals(id)) return e;
4    }
5    return UNKNOWN;
6}
7
Full Screen
copy
1        System.out.println("font-size = "+ele.getCssValue("font-size"));
2        System.out.println("background = "+ele.getCssValue("background"));
3        System.out.println("line-height = "+ele.getCssValue("line-height"));
4        System.out.println("color = "+ele.getCssValue("color"));
5        System.out.println("font-family = "+ele.getCssValue("font-family"));
6
Full Screen
copy
1public WebDriver driver;
2private By bySearchButton = By.name("btnK");
3
4@BeforeClass
5public void setUp() {
6    driver = new FirefoxDriver();
7    driver.get("http://www.google.com");
8}
9
10@Test(priority=1)
11public void getCssValue_ButtonColor()  {
12    WebElement googleSearchBtn = driver.findElement(bySearchButton); 
13    System.out.println("Color of a button before mouse hover: " + googleSearchBtn.getCssValue("color"));
14    Actions action = new Actions(driver);
15    action.moveToElement(googleSearchBtn).perform();
16    System.out.println("Color of a button after mouse hover : " + googleSearchBtn.getCssValue("color"));
17}
18
19@Test(priority=2)
20public void getCssValue_ButtonFontSize() {
21    WebElement googleSearchBtn = driver.findElement(bySearchButton);
22    System.out.println("Font Size of a button " + googleSearchBtn.getCssValue("font-size"));
23}
24
25@Test(priority=3)
26public void getCssValue_ButtonFontWeight(){
27    WebElement googleSearchBtn = driver.findElement(bySearchButton);
28    System.out.println("Font Weight of a button "   +getFontWeight(googleSearchBtn) );
29}
30
31public String getFontWeight(WebElement element) {
32    //Output will return as 400 for font-weight : normal, and 700 for font-weight : bold
33    return element.getCssValue("font-weight");
34}
35
36@AfterClass
37public void tearDown() {
38    driver.quit();
39}
40
Full Screen
copy
1driver.findElement(By.id("by-id")).getCssValue("font-size");//similarly you can use other CSS property such as background-color, font-family etc.
2
Full Screen
copy
1try{
2        WebElement byId = driver.findElement(By.id("by-id"));
3
4        System.out.println(byId.getTagName());
5
6        System.out.println("get the text for web element with id='by-id' ");
7        System.out.println("------------------------------------------------------------");
8        System.out.println(byId.getText());
9        System.out.println("------------------------------------------------------------");
10        System.out.println(byId.getAttribute("id"));
11        System.out.println(byId.getCssValue("font-size"));
12    }
13}
14
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 ById

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)