Best Python code snippet using playwright-python

test_connectivity.py

Source:test_connectivity.py

**...****116** component1 = next(components)**117** with pytest.raises(StopIteration):**118** next(components)**119** assert component1 == set([0, 1, 2, 3, 4, 5])**120**def test_is_connected():**121** # directed**122** g = create_graph(**123** directed=True,**124** allowing_self_loops=False,**125** allowing_multiple_edges=False,**126** weighted=True,**127** )**128** g.add_vertices_from([0, 1])**129** g.add_edge(0, 1)**130** is_connected, components = connectivity.is_connected(g)**131** assert not is_connected**132** g.add_edge(1, 0)**133** is_connected, components = connectivity.is_connected(g)**134** assert is_connected**135** # undirected**136** g = create_graph(**137** directed=False,**138** allowing_self_loops=False,**139** allowing_multiple_edges=False,**140** weighted=True,**141** )**142** g.add_vertices_from([0, 1])**143** g.add_edge(0, 1)**144** is_connected, components = connectivity.is_connected(g)**145** assert is_connected**146**def test_anyhashableg_strongly_gabow():**147** g = create_graph(**148** directed=True,**149** allowing_self_loops=False,**150** allowing_multiple_edges=False,**151** weighted=True,**152** any_hashable=True,**153** )**154** g.add_vertices_from(["0", "1", "2", "3", "4", "5"])**155** g.add_edge("0", "1")**156** g.add_edge("1", "2")**157** g.add_edge("2", "0")**158** g.add_edge("2", "3")**...**

HitBricks.py

Source:HitBricks.py

**1**import collections**2**class Solution(object):**3** def get_connected(self, grid, x, y, n, m, is_connected):**4** stack, result = [(x,y)], []**5** visited = set([(x, y)])**6** **7** while len(stack) > 0:**8** u, v = stack.pop()**9** result.append((u,v))**10** **11** if u == 0 or ((u,v) in is_connected and is_connected[(u,v)]):**12** return True, result**13** **14** for i in [-1,1]:**15** if 0 <= u+i < n and 0 <= v < m and grid[u+i][v] == 1 and (u+i, v) not in visited:**16** stack.append((u+i,v))**17** visited.add((u+i,v))**18** **19** if 0 <= u < n and 0 <= v+i < m and grid[u][v+i] == 1 and (u, v+i) not in visited:**20** stack.append((u,v+i))**21** visited.add((u,v+i))**22** **23** return False, result**24** **25** **26** def get_deleted_neighbors(self, grid, x, y, n, m, is_connected):**27** stack, result = [(x,y)], []**28** visited = set([(x, y)])**29** **30** while len(stack) > 0:**31** u, v = stack.pop()**32** result.append((u,v))**33** **34** for i in [-1,1]:**35** if 0 <= u+i < n and 0 <= v < m and (grid[u+i][v] == 2 or (grid[u+i][v] == 1 and is_connected[(u+i,v)] is False)) and (u+i, v) not in visited:**36** stack.append((u+i,v))**37** visited.add((u+i,v))**38** **39** if 0 <= u < n and 0 <= v+i < m and (grid[u][v+i] == 2 or (grid[u][v+i] == 1 and is_connected[(u,v+i)] is False)) and (u, v+i) not in visited:**40** stack.append((u,v+i))**41** visited.add((u,v+i))**42** **43** return result**44** **45** **46** def drop_bricks(self, grid, x, y, n, m, is_connected):**47** if 0 <= x < n and 0 <= y < m and grid[x][y] == 1:**48** connected, result = self.get_connected(grid, x, y, n, m, is_connected)**49** for x, y in result:**50** if connected is False:**51** grid[x][y] = 2**52** is_connected[(x,y)] = connected**53** **54** **55** def hitBricks(self, grid, hits):**56** n, m = len(grid), len(grid[0])**57** is_connected = dict()**58** **59** for x in range(len(grid)):**60** for y in range(len(grid[0])):**61** if grid[x][y] == 1:**62** is_connected[(x,y)] = False**63** **64** for x, y in hits:**65** if grid[x][y] == 1:**66** grid[x][y] = -1**67** **68** for x, y in hits:**69** if grid[x][y] == -1:**70** self.drop_bricks(grid, x+1, y, n, m, is_connected)**71** self.drop_bricks(grid, x-1, y, n, m, is_connected)**72** self.drop_bricks(grid, x, y+1, n, m, is_connected)**73** self.drop_bricks(grid, x, y-1, n, m, is_connected)**74** **75** out, disconnected_hits = [0]*len(hits), []**76** **77** for i in reversed(range(len(hits))):**78** x, y = hits[i]**79** if grid[x][y] == -1:**80** grid[x][y] = 1**81** **82** connected, _ = self.get_connected(grid, x, y, n, m, is_connected)**83** is_connected[(x,y)] = connected**84** **85** if connected:**86** deleted_neighbors = self.get_deleted_neighbors(grid, x, y, n, m, is_connected)**87** cnts = len(deleted_neighbors)-1**88** for u, v in deleted_neighbors:**89** grid[u][v] = 1**90** is_connected[(u,v)] = connected**91** out[i] = cnts**92** else:**93** disconnected_hits.append(i)**94** **95** for i in disconnected_hits:**96** x, y = hits[i]**97** if is_connected[(x,y)] is False:**98** grid[x][y] = 1**99** **100** deleted_neighbors = self.get_deleted_neighbors(grid, x, y, n, m, is_connected)**101** cnts = len(deleted_neighbors)-1**102** out[i] = cnts**103** **...**

1202. Smallest String With Swaps.py

Source:1202. Smallest String With Swaps.py

**1**import collections**2**#æè·¯ï¼ åä¸æ·±æçæåº**3**class Solution(object):**4** def smallestStringWithSwaps(self, s, pairs):**5** """**6** :type s: str**7** :type pairs: List[List[int]]**8** :rtype: str**9** """**10** graph = collections.defaultdict(dict)**11** for ele in pairs:**12** graph[ele[0]][ele[1]] = True**13** graph[ele[1]][ele[0]] = True**14** self.graph = graph**15** self.used = set()**16** res = []**17** s = [ele for ele in s]**18** self.s = s**19** for index,ele in enumerate(s):**20** min_ele = self.dfs(index, set())**21** s[index],s[min_ele] = s[min_ele], s[index]**22** self.used.add(index)**23** res.append(s[min_ele])**24** return res**25** def dfs(self, index, visited):**26** for ele in self.graph[index]:**27** if self.if_visited(ele) and ele not in visited:**28** visited.add(ele)**29** if ord(self.s[index]) < ord(self.s[ele]):**30** self.dfs(index, visited)**31** else:**32** index = ele**33** self.dfs(ele, visited)**34** return index**35** def if_visited(self, ele):**36** if ele in self.used:**37** return False**38** return True**39**import collections**40**class Solution:**41** def dfs(self, i, is_connected):**42** for neighbour in self.graph[i]:**43** if neighbour not in self.visited:**44** # is_connected.append(self.s[neighbour])**45** is_connected.append(neighbour)**46** self.visited.add(neighbour)**47** self.dfs(neighbour, is_connected)**48** def smallestStringWithSwaps(self, s, pairs):**49** # å»ºå¾**50** graph = collections.defaultdict(list)**51** for ele in pairs:**52** graph[ele[0]].append(ele[1])**53** graph[ele[1]].append(ele[0])**54** self.graph = graph**55** self.visited = set()**56** self.s = s**57** # print(self.graph)**58** res = list(s)**59** for i in range(len(s)):**60** if i not in self.visited:**61** is_connected = []**62** self.dfs(i, is_connected)**63** neighbour = [s[i] for i in is_connected]**64** neighbour.sort()**65** is_connected.sort()**66** for j,ele in zip(is_connected, neighbour):**67** res[j] = ele**68** return "".join(res)**69**class Solution(object):**70** def bfs(self, i, is_connected):**71** if i not in self.visited:**72** self.visited.add(i)**73** is_connected.append(i)**74** queue = collections.deque(self.graph[i])**75** while queue:**76** node = queue.popleft()**77** if node not in self.visited:**78** self.visited.add(node)**79** is_connected.append(node)**80** queue.extend(self.graph[node])**81** def smallestStringWithSwaps(self, s, pairs):**82** """**83** :type s: str**84** :type pairs: List[List[int]]**85** :rtype: str**86** """**87** graph = collections.defaultdict(list)**88** for ele in pairs:**89** graph[ele[0]].append(ele[1])**90** graph[ele[1]].append(ele[0])**91** self.graph = graph**92** self.visited = set()**93** self.s = s**94** res = list(s)**95** for i in range(len(s)):**96** if i not in self.visited:**97** is_connected = []**98** self.bfs(i, is_connected)**99** neighbour = [s[i] for i in is_connected]**100** neighbour.sort()**101** is_connected.sort()**102** for j, ele in zip(is_connected, neighbour):**103** res[j] = ele**104** return "".join(res)**105**if __name__ == '__main__':**106** # s = "dcab"**107** # pairs = [[0,3],[1,2]]**108** s = "dcab"**109** pairs = [[0, 3], [1, 2], [0, 2]]**...**

directed_graph_unweighted_test.py

Source:directed_graph_unweighted_test.py

**...****19** graph.connect("a", "c")**20** graph.connect("b", "d")**21** graph.connect("c", "d")**22** graph.connect("d", "e")**23** self.assertTrue(graph.is_connected("a", "b"))**24** self.assertTrue(graph.is_connected("a", "c"))**25** self.assertTrue(graph.is_connected("b", "d"))**26** self.assertTrue(graph.is_connected("c", "d"))**27** self.assertTrue(graph.is_connected("d", "e"))**28** self.assertFalse(graph.is_connected("a", "e"))**29** self.assertFalse(graph.is_connected("a", "d"))**30** self.assertFalse(graph.is_connected("b", "c"))**31** self.assertFalse(graph.is_connected("b", "e"))**32** self.assertFalse(graph.is_connected("c", "e"))**33** def test_connect_throws_error_if_vertex_not_found(self):**34** graph = DirectedGraphUnweighted()**35** with self.assertRaises(KeyError):**36** graph.connect("a", "b")**37** def test_is_connected_throws_error_if_vertex_not_found(self):**38** graph = DirectedGraphUnweighted()**39** with self.assertRaises(KeyError):**40** graph.is_connected("a", "b")**41** def test_neighbours(self):**42** """**43** a---->b**44** | |**45** v v**46** c---->d---->e**47** """**48** graph = DirectedGraphUnweighted()**49** graph.add_vertex("a")**50** graph.add_vertex("b")**51** graph.add_vertex("c")**52** graph.add_vertex("d")**53** graph.add_vertex("e")**54** graph.connect("a", "b")**...**

LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.

- What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
- How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
- Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
- What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
- Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
- Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
- Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
- Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
- Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
- Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
- Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
- Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Get 100 minutes of automation test minutes FREE!!