Skip to content

Commit 7931fe3

Browse files
adding default ports
1 parent 42a85c4 commit 7931fe3

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

src/server.lua

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,42 @@ WebDriver.newLocalServer = function(props)
3939
if not props.fetch then
4040
error("fetch is required")
4141
end
42-
4342
local selfobj = Heregitage.newMetaObject()
4443
selfobj.private_props_extends(props)
45-
selfobj.private.url = "http://127.0.0.1:"..selfobj.private.port
4644
selfobj.set_meta_method("__gc", Server.__gc)
4745
selfobj.set_meta_method("close", Server.close)
4846
selfobj.set_public_method("newSession", Server.newSession)
47+
48+
if props.port then
49+
selfobj.private.url = "http://127.0.0.1:"..selfobj.private.port
50+
local command = "%s --port=%d &"
51+
command = command:format(props.chromedriver_command, props.port)
52+
print("Starting chromedriver with command: " .. command)
53+
local ok = os.execute(command)
54+
if ok then
55+
error("Failed to start chromedriver with command: " .. command)
56+
end
57+
else
58+
local started = false
59+
for i = 4444, 65535 do
60+
selfobj.private.port = i
61+
selfobj.private.url = "http://127.0.0.1:"..selfobj.private.port
4962

63+
-- Start chromedriver with proper command formatting
64+
local command = "%s --port=%d &"
65+
command = command:format(props.chromedriver_command, selfobj.private.port)
66+
print("Starting chromedriver with command: " .. command)
67+
local ok = os.execute(command)
68+
if ok then
69+
started = true
70+
break
71+
end
72+
end
73+
if not started then
74+
error("Failed to start chromedriver on any port between 4444 and 65535")
75+
end
76+
end
5077

51-
-- Start chromedriver with proper command formatting
52-
local command = "%s --port=%d &"
53-
command = command:format(props.chromedriver_command, props.port)
54-
55-
print("Starting chromedriver with command: " .. command)
56-
os.execute(command)
57-
5878
-- Wait for chromedriver to start
5979
os.execute("sleep 2")
6080

teste.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
local webdriver = require("luaWebDriver")
3+
local luabear = require("luaBear.luaBear")
4+
5+
-- Setup WebDriver server
6+
local server = webdriver.newLocalServer({
7+
fetch = luabear.fetch,
8+
chromedriver_command = "/home/mateus/oui_chrome/chromedriver-linux64/chromedriver",
9+
})
10+
11+
-- Create a new browser session
12+
local session = server.newSession({
13+
binary_location = "/home/mateus/oui_chrome/chrome-linux64/chrome",
14+
})
15+
16+
-- Navigate to a website
17+
session.navegate_to("https://news.ycombinator.com")
18+
19+
-- Find and interact with elements
20+
local big_box = session.get_element_by_id("bigbox")
21+
local td = big_box[1]
22+
local table_1 = td[1]
23+
local tbody = table_1[1]
24+
25+
-- Print all news items
26+
print("=== Hacker News Articles ===")
27+
for i = 1, tbody.get_children_size() do
28+
local tr = tbody.get_element_by_index(i)
29+
local text = tr.get_text()
30+
if text and text ~= "" then
31+
print(i .. ". " .. text)
32+
end
33+
end
34+
while true do end
35+
print("✅ Test completed successfully!")

0 commit comments

Comments
 (0)