Skip to content

Commit

Permalink
API: impl BrowserContext.set_geolocation
Browse files Browse the repository at this point in the history
Warning:

this function has not yet been successfully tested.
  > So far, the test runs have failed to receive location data and instead
  > experiences "error code 2", which [reportedly](https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError/code)
  > represents `POSITION_UNAVAILABLE` - "The acquisition of the geolocation
  > failed because one or several internal sources of position returned an internal error."
  • Loading branch information
coreyti committed Oct 9, 2024
1 parent cb3881d commit bbbef41
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export REPO="$(expand_path .)"
export ERL_AFLAGS="-kernel shell_history enabled" # persistent iex history

# default: true
export PLAYWRIGHT_HEADLESS=false
# export PLAYWRIGHT_HEADLESS=false

# default transport: driver (websocket is the one alternative)
# export PLAYWRIGHT_TRANSPORT=websocket
Expand Down
56 changes: 56 additions & 0 deletions lib/playwright/browser_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,62 @@ defmodule Playwright.BrowserContext do
Channel.post({context, "setExtraHTTPHeaders"}, %{headers: serialize_headers(headers)})
end

@doc """
Sets the context's geolocation.
Passing `nil` emulates position unavailable.
> #### NOTE {: .info}
>
> Consider using `Playwright.BrowserContext.grant_permissions/3` to grant
> permissions for the browser context pages to read geolocation.
> #### WARNING! {: .warning}
>
> As of 2024-10-09, this function has not yet been successfully tested.
> So far, the test runs have failed to receive location data and instead
> experiences "error code 2", which [reportedly](https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError/code)
> represents `POSITION_UNAVAILABLE` - "The acquisition of the geolocation
> failed because one or several internal sources of position returned an internal error."
## Usage
BrowserContext.set_geolocation(context, %{
latitude: 59.95,
longitude: 30.31667
})
## Arguments
| name | | description |
| ------------- | ---------- | ------------------------------- |
| `context` | | The "subject" `BrowserContext`. |
| `geolocation` | | `BrowserContext.geolocation()`. |
### Geolocation settings
| name | | description |
| ----------- | ---------- | ----------------------------------- |
| `latitude` | | Latitude between `-90` and `90`. |
| `lingitude` | | Longitude between `-180` and `180`. |
| `accuracy` | (optional) | Non-negative accuracy value. Defaults to `0`. |
## Returns
- `BrowserContext.t()`
- `{:error, Error. t()}`
"""
@pipe {:set_geolocation, [:context, :geolocation]}
@spec set_geolocation(t(), geolocation() | nil) :: t() | {:error, Error.t()}
def set_geolocation(context, params \\ nil)

def set_geolocation(%BrowserContext{} = context, params) when is_map(params) do
Channel.post({context, :set_geolocation}, params)
end

def set_geolocation(%BrowserContext{} = context, nil) do
Channel.post({context, :set_geolocation})
end

@spec set_offline(t(), boolean()) :: t() | {:error, Error.t()}
def set_offline(%BrowserContext{} = context, offline) do
Expand Down
51 changes: 51 additions & 0 deletions test/api/browser_context_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,57 @@ defmodule Playwright.BrowserContextTest do
end
end

# skip: See documentation comment for `BrowserContext.set_geolocation/2`
@tag :skip
describe "BrowserContext.set_geolocation/2" do
test "on success, returns the 'subject' `BrowserContext`", %{page: page} do
context = Page.context(page)
assert %BrowserContext{} = BrowserContext.set_geolocation(context, nil)
end

test "on failure, returns `{:error, error}`", %{page: page} do
context = Page.context(page)
context = %{context | guid: "bogus"}

assert {:error, %Error{message: "Target page, context or browser has been closed"}} =
BrowserContext.set_geolocation(context, %{})
end

test "...", %{assets: assets, page: page} do

Check failure on line 955 in test/api/browser_context_test.exs

View workflow job for this annotation

GitHub Actions / Build & Test

test BrowserContext.set_geolocation/2 ... (Playwright.BrowserContextTest)
context = Page.context(page)
BrowserContext.grant_permissions(context, ["geolocation"])

BrowserContext.set_geolocation(context, %{latitude: 10, longitude: 10})

Page.goto(page, assets.empty)

geolocation =
Page.evaluate(page, """
async() => new Promise(resolve => navigator.geolocation.getCurrentPosition(position => {
resolve({latitude: position.coords.latitude, longitude: position.coords.longitude});
}))
""")
|> IO.inspect(label: "geolocation")

assert %{latitude: 10, longitude: 10} = geolocation
end
end

describe "BrowserContext.set_geolocation!/2" do
test "on success, returns the 'subject' `BrowserContext`", %{page: page} do
context = Page.context(page)
assert %BrowserContext{} = BrowserContext.set_geolocation!(context, %{latitude: 0, longitude: 0})
end

test "on failure, raises `RuntimeError`", %{page: page} do
assert_raise RuntimeError, "Target page, context or browser has been closed", fn ->
context = Page.context(page)
context = %{context | guid: "bogus"}
BrowserContext.set_geolocation!(context, %{latitude: 0, longitude: 0})
end
end
end

describe "BrowserContext.set_offline/2" do
test "returns 'subject'", %{page: page} do
context = Page.context(page)
Expand Down

0 comments on commit bbbef41

Please sign in to comment.