How to use get method of org.openqa.selenium.grid.graphql.SessionData class

Best Selenium code snippet using org.openqa.selenium.grid.graphql.SessionData.get

Source:GraphqlHandler.java Github

copy

Full Screen

...79 .build();80 graphQl = GraphQL.newGraphQL(schema)81 .preparsedDocumentProvider((executionInput, computeFunction) -> {82 try {83 return cache.get(executionInput.getQuery(), () -> computeFunction.apply(executionInput));84 } catch (ExecutionException e) {85 if (e.getCause() instanceof RuntimeException) {86 throw (RuntimeException) e.getCause();87 } else if (e.getCause() != null) {88 throw new RuntimeException(e.getCause());89 }90 throw new RuntimeException(e);91 }92 })93 .build();94 }95 @Override96 public HttpResponse execute(HttpRequest req) throws UncheckedIOException {97 if (req.getMethod() == OPTIONS) {98 return new HttpResponse();99 }100 try (Span span = newSpanAsChildOf(tracer, req, "grid.status")) {101 HttpResponse response;102 Map<String, Object> inputs = JSON.toType(Contents.string(req), MAP_TYPE);103 Map<String, EventAttributeValue> attributeMap = new HashMap<>();104 attributeMap.put(AttributeKey.LOGGER_CLASS.getKey(),105 EventAttribute.setValue(getClass().getName()));106 HTTP_REQUEST.accept(span, req);107 HTTP_REQUEST_EVENT.accept(attributeMap, req);108 if (!(inputs.get("query") instanceof String)) {109 response = new HttpResponse()110 .setStatus(HTTP_INTERNAL_ERROR)111 .setContent(Contents.utf8String("Unable to find query"));112 HTTP_RESPONSE.accept(span, response);113 HTTP_RESPONSE_EVENT.accept(attributeMap, response);114 attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),115 EventAttribute.setValue("Unable to find query"));116 span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);117 return response;118 }119 String query = (String) inputs.get("query");120 @SuppressWarnings("unchecked")121 Map<String, Object> variables = inputs.get("variables") instanceof Map ?122 (Map<String, Object>) inputs.get("variables") :123 new HashMap<>();124 ExecutionInput executionInput = ExecutionInput.newExecutionInput(query)125 .variables(variables)126 .build();127 ExecutionResult result = graphQl.execute(executionInput);128 if (result.isDataPresent()) {129 response = new HttpResponse()130 .addHeader("Content-Type", JSON_UTF_8)131 .setContent(utf8String(JSON.toJson(result.toSpecification())));132 HTTP_RESPONSE.accept(span, response);133 HTTP_RESPONSE_EVENT.accept(attributeMap, response);134 span.addEvent("Graphql query executed", attributeMap);135 return response;136 }137 response = new HttpResponse()138 .setStatus(HTTP_INTERNAL_ERROR)139 .setContent(utf8String(JSON.toJson(result.getErrors())));140 HTTP_RESPONSE.accept(span, response);141 HTTP_RESPONSE_EVENT.accept(attributeMap, response);142 attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),143 EventAttribute.setValue("Error while executing the query"));144 span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);145 return response;146 }147 }148 private RuntimeWiring buildRuntimeWiring() {149 GridData gridData = new GridData(distributor, newSessionQueue, publicUri, version);150 return RuntimeWiring.newRuntimeWiring()151 .scalar(Types.Uri)152 .scalar(Types.Url)153 .type("GridQuery", typeWiring -> typeWiring154 .dataFetcher("grid", gridData)155 .dataFetcher("sessionsInfo", gridData)156 .dataFetcher("nodesInfo", gridData)157 .dataFetcher("session", new SessionData(distributor)))158 .build();159 }160 private TypeDefinitionRegistry buildTypeDefinitionRegistry() {161 try (InputStream stream = getClass().getResourceAsStream(GRID_SCHEMA)) {162 return new SchemaParser().parse(stream);163 } catch (IOException e) {164 throw new UncheckedIOException(e);165 }166 }167}...

Full Screen

Full Screen

Source:SessionData.java Github

copy

Full Screen

...28import java.util.function.Supplier;29public class SessionData implements DataFetcher {30 private final Supplier<DistributorStatus> distributorStatus;31 public SessionData(Distributor distributor) {32 distributorStatus = Suppliers.memoize(Require.nonNull("Distributor", distributor)::getStatus);33 }34 @Override35 public Object get(DataFetchingEnvironment environment) {36 String sessionId = environment.getArgument("id");37 if (sessionId.isEmpty()) {38 throw new SessionNotFoundException("Session id is empty. A valid session id is required.");39 }40 Set<NodeStatus> nodeStatuses = distributorStatus.get().getNodes();41 SessionInSlot currentSession = findSession(sessionId, nodeStatuses);42 if (currentSession != null) {43 org.openqa.selenium.grid.data.Session session = currentSession.session;44 return new org.openqa.selenium.grid.graphql.Session(45 session.getId().toString(),46 session.getCapabilities(),47 session.getStartTime(),48 session.getUri(),49 currentSession.node.getId().toString(),50 currentSession.node.getUri(),51 currentSession.slot);52 } else {53 throw new SessionNotFoundException("No ongoing session found with the requested session id.",54 sessionId);55 }56 }57 private SessionInSlot findSession(String sessionId, Set<NodeStatus> nodeStatuses) {58 for (NodeStatus status : nodeStatuses) {59 for (Slot slot : status.getSlots()) {60 Optional<org.openqa.selenium.grid.data.Session> session = slot.getSession();61 if (session.isPresent() && sessionId.equals(session.get().getId().toString())) {62 return new SessionInSlot(session.get(), status, slot);63 }64 }65 }66 return null;67 }68 private static class SessionInSlot {69 private final org.openqa.selenium.grid.data.Session session;70 private final NodeStatus node;71 private final Slot slot;72 SessionInSlot(org.openqa.selenium.grid.data.Session session, NodeStatus node, Slot slot) {73 this.session = session;74 this.node = node;75 this.slot = slot;76 }...

Full Screen

Full Screen

get

Using AI Code Generation

copy

Full Screen

1import com.google.gson.Gson2import com.google.gson.reflect.TypeToken3import org.openqa.selenium.grid.graphql.SessionData4import java.io.File5import java.nio.file.Files6import java.nio.file.Paths7import java.util.concurrent.TimeUnit8val gson = Gson()9val sessionData = SessionData()10fun main() {11 val sessionList = sessionData.getSessions()12 val sessionListType = object : TypeToken<List<SessionData.Session>>() {}.type13 val sessionListJson = gson.toJson(sessionList, sessionListType)14 println(sessionListJson)15 val sessionFile = File("session.json")16 sessionFile.writeText(sessionListJson)17 val sessionJson = Files.readString(Paths.get("session.json"))18 val sessionListFromJson = gson.fromJson<List<SessionData.Session>>(sessionJson, sessionListType)19 println(sessionListFromJson)20 val sessionListFromJson2 = gson.fromJson<List<SessionData.Session>>(sessionListJson, sessionListType)21 println(sessionListFromJson2)22}

Full Screen

Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

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

Most used method in SessionData

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful