Skip to content

Commit

Permalink
ft: add smoketests for all evasions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattwmaster58 committed Oct 26, 2024
1 parent 15af6f0 commit d51b310
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 6 deletions.
6 changes: 3 additions & 3 deletions playwright_stealth/js/evasions/chrome.csi.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ if (!("csi" in window.chrome) && window.performance?.timing) {
log("loading chrome.csi.js");
window.chrome.csi = function () {
return {
onloadT: csi_timing.domContentLoadedEventEnd,
startE: csi_timing.navigationStart,
pageT: Date.now() - csi_timing.navigationStart,
onloadT: csi_timing?.domContentLoadedEventEnd,
startE: csi_timing?.navigationStart,
pageT: Date.now() - csi_timing?.navigationStart,
tran: 15, // transition? seems constant
};
};
Expand Down
2 changes: 1 addition & 1 deletion playwright_stealth/js/evasions/chrome.runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ if (!window.chrome) {
const existsAlready = "runtime" in window.chrome;
// `chrome.runtime` is only exposed on secure origins
const isNotSecure = !window.location.protocol.startsWith("https");
if (!(existsAlready || (isNotSecure && !opts.run_on_unsecure_origins))) {
if (!existsAlready && !(isNotSecure && !opts.run_on_unsecure_origins)) {
window.chrome.runtime = {
// There's a bunch of static data in that property which doesn't seem to change,
// we should periodically check for updates: `JSON.stringify(window.chrome.runtime, null, 2)`
Expand Down
15 changes: 13 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,28 @@
from playwright_stealth import Stealth

script_logging = True
browser_types = ["chromium", "firefox"]


@pytest.fixture(params=["chromium", "firefox"])
@pytest.fixture(params=browser_types)
async def hooked_async_browser(request) -> async_api.Browser:
async with Stealth(script_logging=script_logging).use_async(async_playwright()) as ctx:
browser = await ctx[request.param].launch()
yield browser


@pytest.fixture(params=["chromium", "firefox"])
@pytest.fixture(params=browser_types)
def hooked_sync_browser(request) -> sync_api.Browser:
with Stealth(script_logging=script_logging).use_sync(sync_playwright()) as ctx:
browser = ctx[request.param].launch()
yield browser


@pytest.fixture
async def hooked_async_page(hooked_async_browser) -> async_api.Page:
return await hooked_async_browser.new_page()


@pytest.fixture
def hooked_sync_page(request, hooked_async_browser) -> sync_api.Page:
return hooked_async_browser.new_page()
90 changes: 90 additions & 0 deletions tests/test_evasion_smoketests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import playwright
from filelock import AsyncUnixFileLock
from playwright.async_api import async_playwright

from playwright_stealth import Stealth


async def test_chrome_app(hooked_async_page):
await hooked_async_page.goto("http://example.org")
a = await hooked_async_page.evaluate("window.chrome")
b = await hooked_async_page.evaluate("window.chrome.app")


async def test_chrome_csi(hooked_async_page):
await hooked_async_page.goto("http://example.org")
a = await hooked_async_page.evaluate("window.chrome.csi")


async def test_chrome_hairline(hooked_async_page):
await hooked_async_page.goto("http://example.org")
a = await hooked_async_page.evaluate("document.offsetHeight")


async def test_chrome_load_times(hooked_async_page):
await hooked_async_page.goto("http://example.org")
a = await hooked_async_page.evaluate("window.chrome.loadTimes")


async def test_chrome_runtime():
# disabled by default
async with Stealth(chrome_runtime=True).use_async(async_playwright()) as p:
page = await (await p.chromium.launch()).new_page()
await page.goto("https://example.org")
a = await page.evaluate("window.chrome.runtime")


async def test_iframe_contentWindow(hooked_async_page):
await hooked_async_page.goto("http://example.org")
a = await hooked_async_page.evaluate("document.createElement('iframe')")


async def test_media_codecs(hooked_async_page):
await hooked_async_page.goto("http://example.org")
a = await hooked_async_page.evaluate("document.createElement('video').canPlayType('audio/x-m4a')")


async def test_navigator_hardwareConcurrency(hooked_async_page):
await hooked_async_page.goto("http://example.org")
a = await hooked_async_page.evaluate("navigator.hardwareConcurrency")


async def test_navigator_languages(hooked_async_page):
await hooked_async_page.goto("http://example.org")
a = await hooked_async_page.evaluate("navigator.languages")


async def test_navigator_permissions(hooked_async_page):
await hooked_async_page.goto("http://example.org")
a = await hooked_async_page.evaluate("navigator.permissions")


async def test_navigator_platform(hooked_async_page):
await hooked_async_page.goto("http://example.org")
a = await hooked_async_page.evaluate("navigator.platform")


async def test_navigator_plugins(hooked_async_page):
await hooked_async_page.goto("http://example.org")
a = await hooked_async_page.evaluate("navigator.plugins")


async def test_navigator_userAgent(hooked_async_page):
await hooked_async_page.goto("http://example.org")
a = await hooked_async_page.evaluate("navigator.userAgent")


async def test_navigator_vendor(hooked_async_page):
await hooked_async_page.goto("http://example.org")
a = await hooked_async_page.evaluate("navigator.vendor")


async def test_navigator_webdriver(hooked_async_page):
await hooked_async_page.goto("http://example.org")
a = await hooked_async_page.evaluate("navigator.webdriver")


async def test_webgl_vendor(hooked_async_page):
await hooked_async_page.goto("http://example.org")
a = await hooked_async_page.evaluate("WebGLRenderingContext.getParameter")
b = await hooked_async_page.evaluate("WebGL2RenderingContext.getParameter")

0 comments on commit d51b310

Please sign in to comment.