How to use EventListener class of org.openqa.selenium.events package

Best Selenium code snippet using org.openqa.selenium.events.EventListener

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
20import org.openqa.selenium.events.Event;
21import org.openqa.selenium.events.EventListener;
22import org.openqa.selenium.events.EventName;
23import org.openqa.selenium.internal.Require;
24
25import java.util.function.Consumer;
26
27public class NodeStatusEvent extends Event {
28
29  private static final EventName NODE_STATUS = new EventName("node-status");
30
31  public NodeStatusEvent(NodeStatus status) {
32    super(NODE_STATUS, Require.nonNull("Node status", status));
33  }
34
35  public static EventListener<NodeStatus> listener(Consumer<NodeStatus> handler) {
36    Require.nonNull("Handler", handler);
37
38    return new EventListener<NodeStatus>(NODE_STATUS, NodeStatus.class, handler);
39  }
40}
41
Full Screen
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
20import org.openqa.selenium.events.Event;
21import org.openqa.selenium.events.EventListener;
22import org.openqa.selenium.events.EventName;
23import org.openqa.selenium.internal.Require;
24
25import java.util.function.Consumer;
26
27public class NodeAddedEvent extends Event {
28
29  private static final EventName NODE_ADDED = new EventName("node-added");
30
31  public NodeAddedEvent(NodeId nodeId) {
32    super(NODE_ADDED, nodeId);
33  }
34
35  public static EventListener<NodeId> listener(Consumer<NodeId> handler) {
36    Require.nonNull("Handler", handler);
37
38    return new EventListener<>(NODE_ADDED, NodeId.class, handler);
39  }
40}
41
Full Screen
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
20import org.openqa.selenium.events.Event;
21import org.openqa.selenium.events.EventListener;
22import org.openqa.selenium.events.EventName;
23import org.openqa.selenium.internal.Require;
24
25import java.util.function.Consumer;
26
27public class NodeDrainComplete extends Event {
28  private static final EventName NODE_DRAIN_COMPLETE = new EventName("node-drain-complete");
29
30  public NodeDrainComplete(NodeId id) {
31    super(NODE_DRAIN_COMPLETE, id);
32  }
33
34  public static EventListener<NodeId> listener(Consumer<NodeId> handler) {
35    Require.nonNull("Handler", handler);
36
37    return new EventListener<>(NODE_DRAIN_COMPLETE, NodeId.class, handler);
38  }
39}
40
Full Screen
copy
1@Component
2    public class AppStartupRunner implements ApplicationRunner {
3
4    @Override
5    public void run(ApplicationArguments args) throws Exception {
6        //some logic here
7    }
8}
9
Full Screen
copy
1@RequiredArgsConstructor
2@Component
3@Slf4j
4public class BeerLoader implements CommandLineRunner {
5    //declare 
6
7    @Override
8    public void run(String... args) throws Exception {
9        //some code here 
10
11    }
12
Full Screen
copy
1public classs SpringBootApplication implements CommandLineRunner{
2
3    @Override
4        public void run(String... arg0) throws Exception {
5        // write your logic here 
6
7        }
8}
9
Full Screen
copy
1@Bean
2public CommandLineRunner commandLineRunner() {
3   return (args) -> System.out.println("Hello World");
4}
5
Full Screen
copy
1@Component
2public class MyCommandLineRunner implements CommandLineRunner {
3
4    @Override
5    public void run(String... args) throws Exception {
6        System.out.println("Hello World");
7    }
8}
9
Full Screen
copy
1 @Bean
2 @Order(1)
3 public CommandLineRunner commandLineRunner() {
4    return (args) -> System.out.println("Hello World, Order 1");
5 }
6
7 @Bean
8 @Order(2)
9 public CommandLineRunner commandLineRunner() {
10    return (args) -> System.out.println("Hello World, Order 2");
11 }
12
Full Screen
copy
1@EventListener(ContextRefreshedEvent.class)
2public void execute() {
3    if(alreadyDone) {
4      return;
5    }
6    System.out.println("hello world");
7} 
8
Full Screen
copy
1 @Component
2public class OnStartServer implements ApplicationListener<ContextRefreshedEvent> {
3
4    @Override
5    public void onApplicationEvent(ContextRefreshedEvent arg0) {
6                // EXECUTE YOUR CODE HERE 
7    }
8}
9
Full Screen
copy
1@Component
2public class CommandLineAppStartupRunner implements CommandLineRunner {
3    private static final Logger logger = LoggerFactory.getLogger(CommandLineAppStartupRunner.class);
4
5    @Override
6    public void run(String...args) throws Exception {
7        logger.info("Application started with command-line arguments: {} . \n To kill this application, press Ctrl + C.", Arrays.toString(args));
8    }
9}
10
Full Screen
copy
1@EventListener 
2fun doSomethingAfterStartup(event: ApplicationReadyEvent) {
3    System.out.println("hello world, I have just started up");
4}
5
Full Screen
copy
1@Configuration
2public class InitialDataConfiguration {
3
4    @PostConstruct
5    public void postConstruct() {
6        System.out.println("Started after Spring boot application !");
7    }
8
9}
10
Full Screen
copy
1@Configuration
2public class InitialDataConfiguration {
3
4    @Bean
5    CommandLineRunner runner() {
6        return args -> {
7            System.out.println("CommandLineRunner running in the UnsplashApplication class...");
8        };
9    }
10}
11
Full Screen
copy
1@Bean
2public SmartInitializingSingleton importProcessor() {
3    return () -> {
4        doStuff();
5    };
6
7}
8
Full Screen
copy
1import org.springframework.boot.ApplicationRunner;
2
3@Component
4public class ServerInitializer implements ApplicationRunner {
5
6    @Override
7    public void run(ApplicationArguments applicationArguments) throws Exception {
8
9        //code goes here
10
11    }
12}
13
Full Screen
copy
1@Component
2public class Monitor {
3    @Autowired private SomeService service
4
5    @PostConstruct
6    public void init(){
7        // start your monitoring in here
8    }
9}
10
Full Screen
copy
1@Component
2public class ApplicationStartup 
3implements ApplicationListener<ApplicationReadyEvent> {
4
5  /**
6   * This event is executed as late as conceivably possible to indicate that 
7   * the application is ready to service requests.
8   */
9  @Override
10  public void onApplicationEvent(final ApplicationReadyEvent event) {
11
12    // here your code ...
13
14    return;
15  }
16}
17
Full Screen
copy
1@Configuration
2@EnableAutoConfiguration
3@ComponentScan
4public class Application extends SpringBootServletInitializer {
5
6    @SuppressWarnings("resource")
7    public static void main(final String[] args) {
8        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
9
10        context.getBean(Table.class).fillWithTestdata(); // <-- here
11    }
12}
13
Full Screen
copy
1@EventListener(ApplicationReadyEvent.class)
2public void doSomethingAfterStartup() {
3    System.out.println("hello world, I have just started up");
4}
5
Full Screen
copy
1public static void sendData(int position)
2{
3     ......
4}
5
Full Screen
copy
1@Override
2public void onClick(View v) 
3{
4     // TODO Auto-generated method stub
5     HomeFragment.sendData(position)
6     ........    
7}
8
Full Screen
copy
1MyFragment extends Fragment implements CustomAdapter.EventListener {
2
3    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
4
5        CustomAdapter adapter = new CustomAdapter(..., this);
6
7    }
8
9    void onEvent(int data) {
10        doSomething(data);
11    }
12
13}
14
15CustomAdapter extends BaseAdapter {
16
17    EventListener listener; 
18
19    public interface EventListener {
20        void onEvent(int data);   
21    }
22
23    public CustomAdapter(..., EventListener listener) {
24        this.listener = listener; 
25    }
26
27    ...
28
29}
30
Full Screen
copy
1customAdapter = new CustomAdapter(myContext, android.R.layout.simple_list_item_1, getList, HomeFragment.this);
2
Full Screen
copy
1public CustomAdapter(Context context, int id, HomeFragment fragment) {
2    this.fragment = fragment;
3}
4
Full Screen
copy
1fragment.doSomething();
2
Full Screen
copy
1import java.awt.BorderLayout;
2import java.awt.Color;
3import java.awt.Component;
4import java.awt.EventQueue;
5import java.awt.Graphics;
6import java.awt.Graphics2D;
7import java.awt.RenderingHints;
8import java.awt.event.ActionEvent;
9import java.awt.event.ActionListener;
10import java.util.Observable;
11import java.util.Observer;
12import java.util.Random;
13import javax.swing.Icon;
14import javax.swing.JButton;
15import javax.swing.JFrame;
16import javax.swing.JLabel;
17import javax.swing.JPanel;
18
19/**
20 * @see https://stackoverflow.com/q/3066590/230513
21 * 15-Mar-2011 r8 https://stackoverflow.com/questions/5274962
22 * 26-Mar-2013 r17 per comment
23 */
24public class MVCGame implements Runnable {
25
26    public static void main(String[] args) {
27        EventQueue.invokeLater(new MVCGame());
28    }
29
30    @Override
31    public void run() {
32        JFrame f = new JFrame();
33        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
34        f.add(new MainPanel());
35        f.pack();
36        f.setLocationRelativeTo(null);
37        f.setVisible(true);
38    }
39}
40
41class MainPanel extends JPanel {
42
43    public MainPanel() {
44        super(new BorderLayout());
45        Model model = new Model();
46        View view = new View(model);
47        Control control = new Control(model, view);
48        JLabel label = new JLabel("Guess what color!", JLabel.CENTER);
49        this.add(label, BorderLayout.NORTH);
50        this.add(view, BorderLayout.CENTER);
51        this.add(control, BorderLayout.SOUTH);
52    }
53}
54
55/**
56 * Control panel
57 */
58class Control extends JPanel {
59
60    private Model model;
61    private View view;
62    private JButton reset = new JButton("Reset");
63
64    public Control(Model model, View view) {
65        this.model = model;
66        this.view = view;
67        this.add(reset);
68        reset.addActionListener(new ButtonHandler());
69    }
70
71    private class ButtonHandler implements ActionListener {
72
73        @Override
74        public void actionPerformed(ActionEvent e) {
75            String cmd = e.getActionCommand();
76            if ("Reset".equals(cmd)) {
77                model.reset();
78            }
79        }
80    }
81}
82
83/**
84 * View
85 */
86class View extends JPanel {
87
88    private static final String s = "Click a button.";
89    private Model model;
90    private ColorIcon icon = new ColorIcon(80, Color.gray);
91    private JLabel label = new JLabel(s, icon, JLabel.CENTER);
92
93    public View(Model model) {
94        super(new BorderLayout());
95        this.model = model;
96        label.setVerticalTextPosition(JLabel.BOTTOM);
97        label.setHorizontalTextPosition(JLabel.CENTER);
98        this.add(label, BorderLayout.CENTER);
99        this.add(genButtonPanel(), BorderLayout.SOUTH);
100        model.addObserver(new ModelObserver());
101    }
102
103    private JPanel genButtonPanel() {
104        JPanel panel = new JPanel();
105        for (Piece p : Piece.values()) {
106            PieceButton pb = new PieceButton(p);
107            pb.addActionListener(new ButtonHandler());
108            panel.add(pb);
109        }
110        return panel;
111    }
112
113    private class ModelObserver implements Observer {
114
115        @Override
116        public void update(Observable o, Object arg) {
117            if (arg == null) {
118                label.setText(s);
119                icon.color = Color.gray;
120            } else {
121                if ((Boolean) arg) {
122                    label.setText("Win!");
123                } else {
124                    label.setText("Keep trying.");
125                }
126            }
127        }
128    }
129
130    private class ButtonHandler implements ActionListener {
131
132        @Override
133        public void actionPerformed(ActionEvent e) {
134            PieceButton pb = (PieceButton) e.getSource();
135            icon.color = pb.piece.color;
136            label.repaint();
137            model.check(pb.piece);
138        }
139    }
140
141    private static class PieceButton extends JButton {
142
143        Piece piece;
144
145        public PieceButton(Piece piece) {
146            this.piece = piece;
147            this.setIcon(new ColorIcon(16, piece.color));
148        }
149    }
150
151    private static class ColorIcon implements Icon {
152
153        private int size;
154        private Color color;
155
156        public ColorIcon(int size, Color color) {
157            this.size = size;
158            this.color = color;
159        }
160
161        @Override
162        public void paintIcon(Component c, Graphics g, int x, int y) {
163            Graphics2D g2d = (Graphics2D) g;
164            g2d.setRenderingHint(
165                RenderingHints.KEY_ANTIALIASING,
166                RenderingHints.VALUE_ANTIALIAS_ON);
167            g2d.setColor(color);
168            g2d.fillOval(x, y, size, size);
169        }
170
171        @Override
172        public int getIconWidth() {
173            return size;
174        }
175
176        @Override
177        public int getIconHeight() {
178            return size;
179        }
180    }
181}
182
183/**
184 * Model
185 */
186class Model extends Observable {
187
188    private static final Random rnd = new Random();
189    private static final Piece[] pieces = Piece.values();
190    private Piece hidden = init();
191
192    private Piece init() {
193        return pieces[rnd.nextInt(pieces.length)];
194    }
195
196    public void reset() {
197        hidden = init();
198        setChanged();
199        notifyObservers();
200    }
201
202    public void check(Piece guess) {
203        setChanged();
204        notifyObservers(guess.equals(hidden));
205    }
206}
207
208enum Piece {
209
210    Red(Color.red), Green(Color.green), Blue(Color.blue);
211    public Color color;
212
213    private Piece(Color color) {
214        this.color = color;
215    }
216}
217
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 EventListener

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)