Source:How to manage view updates from controllers in a Java Swing app
ArrayList<User> usersArrayList = new ArrayList<User>();
Collection<User> userCollection = new HashSet<User>(usersArrayList);
Best Selenium code snippet using org.openqa.selenium.interactions.PauseAction
Source:ActionsTest.java
1// Licensed to the Software Freedom Conservancy (SFC) under one2// or more contributor license agreements. See the NOTICE file3// distributed with this work for additional information4// regarding copyright ownership. The SFC licenses this file5// to you under the Apache License, Version 2.0 (the6// "License"); you may not use this file except in compliance7// with the License. You may obtain a copy of the License at8//9// http://www.apache.org/licenses/LICENSE-2.010//11// Unless required by applicable law or agreed to in writing,12// software distributed under the License is distributed on an13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14// KIND, either express or implied. See the License for the15// specific language governing permissions and limitations16// under the License.17package org.openqa.selenium.interactions;18import static org.junit.Assert.assertEquals;19import static org.mockito.Mockito.inOrder;20import static org.mockito.Mockito.mock;21import static org.mockito.Mockito.times;22import static org.mockito.Mockito.when;23import static org.mockito.Mockito.withSettings;24import org.junit.Before;25import org.junit.Test;26import org.junit.runner.RunWith;27import org.junit.runners.JUnit4;28import org.mockito.ArgumentCaptor;29import org.mockito.InOrder;30import org.mockito.Mock;31import org.mockito.Mockito;32import org.mockito.MockitoAnnotations;33import org.openqa.selenium.Keys;34import org.openqa.selenium.WebDriver;35import org.openqa.selenium.WebElement;36import org.openqa.selenium.interactions.internal.Coordinates;37import org.openqa.selenium.interactions.internal.Locatable;38import java.util.Collection;39import java.util.HashMap;40import java.util.List;41import java.util.Map;42/**43 * Tests the builder for advanced user interaction, the Actions class.44 */45@RunWith(JUnit4.class)46public class ActionsTest {47 @Mock private Mouse mockMouse;48 @Mock private Keyboard mockKeyboard;49 @Mock private Coordinates mockCoordinates;50 private WebElement dummyLocatableElement;51 private WebDriver driver;52 @Before53 public void setUp() {54 MockitoAnnotations.initMocks(this);55 dummyLocatableElement = mockLocatableElementWithCoordinates(mockCoordinates);56 driver = mock(WebDriver.class, withSettings().extraInterfaces(HasInputDevices.class));57 when(((HasInputDevices) driver).getKeyboard()).thenReturn(mockKeyboard);58 when(((HasInputDevices) driver).getMouse()).thenReturn(mockMouse);59 }60 @Test61 public void creatingAllKeyboardActions() {62 new Actions(driver).keyDown(Keys.SHIFT).sendKeys("abc").keyUp(Keys.CONTROL).perform();63 InOrder order = inOrder(mockMouse, mockKeyboard, mockCoordinates);64 order.verify(mockKeyboard).pressKey(Keys.SHIFT);65 order.verify(mockKeyboard).sendKeys("abc");66 order.verify(mockKeyboard).releaseKey(Keys.CONTROL);67 order.verifyNoMoreInteractions();68 }69 @Test(expected = IllegalArgumentException.class)70 public void throwsIllegalArgumentExceptionIfKeysNull() {71 new Actions(driver).sendKeys().perform();72 }73 @Test(expected = IllegalArgumentException.class)74 public void throwsIllegalArgumentExceptionOverridenIfKeysNull() {75 new Actions(driver).sendKeys(dummyLocatableElement).perform();76 }77 @Test78 public void providingAnElementToKeyboardActions() {79 new Actions(driver).keyDown(dummyLocatableElement, Keys.SHIFT).perform();80 InOrder order = inOrder(mockMouse, mockKeyboard, mockCoordinates);81 order.verify(mockMouse).click(mockCoordinates);82 order.verify(mockKeyboard).pressKey(Keys.SHIFT);83 order.verifyNoMoreInteractions();84 }85 @Test86 public void supplyingIndividualElementsToKeyboardActions() {87 final Coordinates dummyCoordinates2 = mock(Coordinates.class, "dummy2");88 final Coordinates dummyCoordinates3 = mock(Coordinates.class, "dummy3");89 final WebElement dummyElement2 = mockLocatableElementWithCoordinates(dummyCoordinates2);90 final WebElement dummyElement3 = mockLocatableElementWithCoordinates(dummyCoordinates3);91 new Actions(driver)92 .keyDown(dummyLocatableElement, Keys.SHIFT)93 .sendKeys(dummyElement2, "abc")94 .keyUp(dummyElement3, Keys.CONTROL)95 .perform();96 InOrder order = inOrder(97 mockMouse,98 mockKeyboard,99 mockCoordinates,100 dummyCoordinates2,101 dummyCoordinates3);102 order.verify(mockMouse).click(mockCoordinates);103 order.verify(mockKeyboard).pressKey(Keys.SHIFT);104 order.verify(mockMouse).click(dummyCoordinates2);105 order.verify(mockKeyboard).sendKeys("abc");106 order.verify(mockMouse).click(dummyCoordinates3);107 order.verify(mockKeyboard).releaseKey(Keys.CONTROL);108 order.verifyNoMoreInteractions();109 }110 @Test111 public void creatingAllMouseActions() {112 new Actions(driver)113 .clickAndHold(dummyLocatableElement)114 .release(dummyLocatableElement)115 .click(dummyLocatableElement)116 .doubleClick(dummyLocatableElement)117 .moveToElement(dummyLocatableElement)118 .contextClick(dummyLocatableElement)119 .perform();120 InOrder order = inOrder(mockMouse, mockKeyboard, mockCoordinates);121 order.verify(mockMouse).mouseMove(mockCoordinates);122 order.verify(mockMouse).mouseDown(mockCoordinates);123 order.verify(mockMouse).mouseMove(mockCoordinates);124 order.verify(mockMouse).mouseUp(mockCoordinates);125 order.verify(mockMouse).mouseMove(mockCoordinates);126 order.verify(mockMouse).click(mockCoordinates);127 order.verify(mockMouse).mouseMove(mockCoordinates);128 order.verify(mockMouse).doubleClick(mockCoordinates);129 // Move twice; oce for moveToElement, once for contextClick.130 order.verify(mockMouse, times(2)).mouseMove(mockCoordinates);131 order.verify(mockMouse).contextClick(mockCoordinates);132 order.verifyNoMoreInteractions();133 }134 @SuppressWarnings({"unchecked", "rawtypes"})135 @Test136 public void testCtrlClick() {137 WebDriver driver = mock(WebDriver.class, withSettings().extraInterfaces(Interactive.class));138 ArgumentCaptor<Collection<Sequence>> sequenceCaptor =139 ArgumentCaptor.forClass((Class) Collection.class);140 Mockito.doNothing().when((Interactive) driver).perform(sequenceCaptor.capture());141 new Actions(driver)142 .keyDown(Keys.CONTROL)143 .click()144 .keyUp(Keys.CONTROL)145 .perform();146 Collection<Sequence> sequence = sequenceCaptor.getValue();147 assertEquals(2, sequence.size());148 // get mouse and keyboard sequences149 Map<String, Object>[] sequencesJson = sequence.stream().map(Sequence::toJson).toArray(HashMap[]::new);150 Map<String, Object> mouseSequence = sequencesJson[0];151 Map<String, Object> keyboardSequence;152 if (!mouseSequence.get("type").equals("pointer")) {153 mouseSequence = sequencesJson[1];154 keyboardSequence = sequencesJson[0];155 }156 else {157 keyboardSequence = sequencesJson[1];158 }159 assertEquals("pointer", mouseSequence.get("type"));160 List<Map<?,?>> mouseActions = (List<Map<?,?>>) mouseSequence.get("actions");161 assertEquals(4, mouseActions.size());162 assertEquals("key", keyboardSequence.get("type"));163 List<Map<?, ?>> keyboardActions = (List<Map<?, ?>>) keyboardSequence.get("actions");164 assertEquals(4, keyboardActions.size());165 // Mouse pauses as key goes down166 Map<?, ?> pauseAction = mouseActions.get(0);167 assertEquals("pause", pauseAction.get("type"));168 assertEquals(0L, pauseAction.get("duration"));169 Map<?, ?> keyDownAction = keyboardActions.get(0);170 assertEquals("keyDown", keyDownAction.get("type"));171 assertEquals(Keys.CONTROL.toString(), keyDownAction.get("value"));172 // Mouse goes down, so keyboard pauses173 Map<?, ?> pointerDownAction = mouseActions.get(1);174 assertEquals("pointerDown", pointerDownAction.get("type"));175 assertEquals(0, pointerDownAction.get("button"));176 pauseAction = keyboardActions.get(1);177 assertEquals("pause", pauseAction.get("type"));178 assertEquals(0L, pauseAction.get("duration"));179 // Mouse goes up, so keyboard pauses180 Map<?, ?> pointerUpAction = mouseActions.get(2);181 assertEquals("pointerUp", pointerUpAction.get("type"));182 assertEquals(0, pointerUpAction.get("button"));183 pauseAction = keyboardActions.get(2);184 assertEquals("pause", pauseAction.get("type"));185 assertEquals(0L, pauseAction.get("duration"));186 // Mouse pauses as keyboard releases key187 pauseAction = mouseActions.get(3);188 assertEquals("pause", pauseAction.get("type"));189 assertEquals(0L, pauseAction.get("duration"));190 Map<?, ?> keyUpAction = keyboardActions.get(3);191 assertEquals("keyUp", keyUpAction.get("type"));192 assertEquals(Keys.CONTROL.toString(), keyUpAction.get("value"));193 }194 private WebElement mockLocatableElementWithCoordinates(Coordinates coord) {195 WebElement element = mock(WebElement.class,196 withSettings().extraInterfaces(Locatable.class));197 when(((Locatable) element).getCoordinates()).thenReturn(coord);198 return element;199 }200}...
Source:PauseAction.java
2import com.google.common.collect.ImmutableList;3import java.time.Duration;4import java.util.List;5@Deprecated6public class PauseAction7 implements Action, IsInteraction8{9 private final long pause;10 11 public PauseAction(long pause)12 {13 this.pause = pause;14 }15 16 public void perform() {17 try {18 Thread.sleep(pause);19 }20 catch (InterruptedException localInterruptedException) {}21 }22 23 public List<Interaction> asInteractions(PointerInput mouse, KeyInput keyboard)24 {25 return ImmutableList.of(new Pause(keyboard, Duration.ofMillis(pause)));...
PauseAction
Using AI Code Generation
1import org.openqa.selenium.interactions.Actions;2import org.openqa.selenium.interactions.Action;3import org.openqa.selenium.interactions.PauseAction;4Actions builder = new Actions(driver);5 .moveToElement(element)6 .pause(2000)7 .click()8 .build();9seriesOfActions.perform();10import org.openqa.selenium.support.ui.Pause;11import org.openqa.selenium.support.ui.Wait;12Wait wait = new FluentWait(driver)13 .withTimeout(10, SECONDS)14 .pollingEvery(1, SECONDS)15 .ignoring(NoSuchElementException.class);16wait.until(new Pause(2, TimeUnit.SECONDS));17import java.util.concurrent.TimeUnit;18driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);19Thread.sleep(2000);20import org.openqa.selenium.support.ui.WebDriverWait;21import org.openqa.selenium.support.ui.ExpectedConditions;22WebDriverWait wait = new WebDriverWait(driver, 10);23wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementId")));24import org.openqa.selenium.support.ui.WebDriverWait;25import org.openqa.selenium.support.ui.ExpectedConditions;26WebDriverWait wait = new WebDriverWait(driver, 10);27wait.until(ExpectedConditions.elementToBeClickable(By.id("elementId")));28import org.openqa.selenium.support.ui.WebDriverWait;29import org.openqa.selenium.support.ui.ExpectedConditions;30WebDriverWait wait = new WebDriverWait(driver, 10);31wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("elementId")));32import org.openqa.selenium.support.ui.WebDriverWait;33import org.openqa.selenium.support.ui.ExpectedConditions;34WebDriverWait wait = new WebDriverWait(driver, 10);35wait.until(ExpectedConditions.alertIsPresent());36import org.openqa.selenium.support.ui.WebDriverWait;37import org.openqa.selenium.support.ui.ExpectedConditions;38WebDriverWait wait = new WebDriverWait(driver, 10);39wait.until(ExpectedConditions.presenceOfElementLocated(By.id("elementId")));40import org.openqa.selenium.support.ui.WebDriverWait;41import org.openqa
PauseAction
Using AI Code Generation
1Actions actions = new Actions(driver);2actions.moveToElement(element).pause(1000).perform();3Actions actions = new Actions(driver);4actions.moveToElement(element).perform();5Thread.sleep(1000);6Actions actions = new Actions(driver);7actions.moveToElement(element).perform();8WebDriverWait wait = new WebDriverWait(driver, 10);9wait.until(ExpectedConditions.visibilityOf(element));10Actions actions = new Actions(driver);11actions.moveToElement(element).perform();12FluentWait wait = new FluentWait(driver);13wait.withTimeout(10, TimeUnit.SECONDS);14wait.until(ExpectedConditions.visibilityOf(element));15Actions actions = new Actions(driver);16actions.moveToElement(element).perform();17Wait wait = new Wait(driver);18wait.withTimeout(10, TimeUnit.SECONDS);19wait.until(ExpectedConditions.visibilityOf(element));20Actions actions = new Actions(driver);21actions.moveToElement(element).perform();22ExpectedCondition wait = new ExpectedCondition(driver);23wait.withTimeout(10, TimeUnit.SECONDS);24wait.until(ExpectedConditions.visibilityOf(element));25Actions actions = new Actions(driver);26actions.moveToElement(element).perform();27ExpectedConditions wait = new ExpectedConditions(driver);28wait.withTimeout(10, TimeUnit.SECONDS);29wait.until(ExpectedConditions.visibilityOf(element));30Actions actions = new Actions(driver);31actions.moveToElement(element).perform();32Wait wait = new Wait(driver);33wait.withTimeout(10, TimeUnit.SECONDS);34wait.until(ExpectedConditions.visibilityOf(element));35Actions actions = new Actions(driver);36actions.moveToElement(element).perform();37ExpectedCondition wait = new ExpectedCondition(driver);38wait.withTimeout(10, TimeUnit.SECONDS);39wait.until(ExpectedConditions.visibilityOf(element));40Actions actions = new Actions(driver);41actions.moveToElement(element).perform();42ExpectedConditions wait = new ExpectedConditions(driver);43wait.withTimeout(10, TimeUnit.SECONDS);44wait.until(ExpectedConditions.visibilityOf(element));
PauseAction
Using AI Code Generation
1Actions action = new Actions(driver);2PauseAction pause = new PauseAction(Duration.ofSeconds(2));3action.pause(pause).perform();4WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));5wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("elementId")));6Thread.sleep(2000);7FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)8.withTimeout(Duration.ofSeconds(2))9.pollingEvery(Duration.ofMillis(500))10.ignoring(NoSuchElementException.class);11wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("elementId")));12WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));13wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("elementId")));14Thread.sleep(2000);15FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)16.withTimeout(Duration.ofSeconds(2))17.pollingEvery(Duration.ofMillis(500))18.ignoring(NoSuchElementException.class);19wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("elementId")));20WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));21wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("elementId")));22Thread.sleep(2000);23FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)24.withTimeout(Duration.ofSeconds(2))25.pollingEvery(Duration.ofMillis(500))
1 ArrayList<User> usersArrayList = new ArrayList<User>();23 Collection<User> userCollection = new HashSet<User>(usersArrayList);4
Source: Make text in JButton not visible
1public <E> List<E> collectionToList(Collection<E> collection)2{3 return (collection instanceof List) ? (List<E>) collection : new ArrayList<E>(collection);4}5
1List<String> will_fail = (List<String>)Collections.unmodifiableCollection(new ArrayList<String>());2
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.
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.
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.
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.
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.
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.
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.
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.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!