Path: blob/trunk/py/test/selenium/webdriver/common/api_request_context_tests.py
10193 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617import json18import tempfile19from pathlib import Path2021import pytest2223from selenium.webdriver.common.api_request_context import APIRequestContext, APIRequestFailure242526@pytest.fixture(autouse=True)27def setup(driver, pages):28driver.get(pages.url("simpleTest.html"))29driver.delete_all_cookies()303132def test_request_initialized(driver):33assert driver.request is not None343536def test_request_returns_same_instance(driver):37first = driver.request38second = driver.request39assert first is second404142def test_get_request(driver, pages):43response = driver.request.get(pages.url("simpleTest.html"))44assert response.status == 20045assert response.ok46assert "html" in response.text().lower()474849def test_get_nonexistent_page(driver, pages):50response = driver.request.get(pages.url("nonexistent_page_xyz.html"))51assert response.status == 40452assert not response.ok535455def test_response_status_text(driver, pages):56response = driver.request.get(pages.url("simpleTest.html"))57assert response.status_text == "OK"58response_404 = driver.request.get(pages.url("nonexistent_page_xyz.html"))59# The webserver sends a custom reason phrase with send_error(),60# so check it's non-empty rather than an exact string.61assert response_404.status_text62assert "Not Found" in response_404.status_text636465def test_head_request(driver, pages):66response = driver.request.head(pages.url("simpleTest.html"))67assert response.status == 20068assert response.body() == b""697071def test_post_json(driver, pages):72response = driver.request.post(73pages.url("echo_body"),74json_data={"key": "value"},75)76assert response.status == 20077body = json.loads(response.text())78assert body["key"] == "value"798081def test_post_form_data(driver, pages):82response = driver.request.post(83pages.url("echo_body"),84data={"field": "value"},85)86assert response.status == 20087assert "field=value" in response.text()888990def test_post_form_kwarg(driver, pages):91response = driver.request.post(92pages.url("echo_body"),93form={"username": "testuser", "password": "testpass"},94)95assert response.status == 20096text = response.text()97assert "username=testuser" in text98assert "password=testpass" in text99100101def test_browser_cookies_sent_with_request(driver, pages):102driver.add_cookie({"name": "test_cookie", "value": "hello123"})103response = driver.request.get(pages.url("echo_headers"))104assert response.status == 200105assert "hello123" in response.text()106107108def test_response_cookies_synced_to_browser(driver, pages):109driver.request.get(pages.url("set_cookie?name=api_cookie&value=synced"))110cookie = driver.get_cookie("api_cookie")111assert cookie is not None112assert cookie["value"] == "synced"113114115def test_response_json(driver, pages):116response = driver.request.get(pages.url("echo_json"))117data = response.json()118assert isinstance(data, dict)119assert data["status"] == "ok"120121122def test_response_text(driver, pages):123response = driver.request.get(pages.url("echo_json"))124text = response.text()125assert isinstance(text, str)126assert "ok" in text127128129def test_response_body_bytes(driver, pages):130response = driver.request.get(pages.url("echo_json"))131body = response.body()132assert isinstance(body, bytes)133assert b"ok" in body134135136def test_response_dispose(driver, pages):137response = driver.request.get(pages.url("echo_json"))138assert len(response.body()) > 0139response.dispose()140assert response.body() == b""141142143def test_response_headers(driver, pages):144response = driver.request.get(pages.url("echo_json"))145assert "content-type" in response.headers146147148def test_custom_headers(driver, pages):149response = driver.request.get(150pages.url("echo_headers"),151headers={"X-Custom-Header": "custom_value_123"},152)153assert response.status == 200154assert "custom_value_123" in response.text()155156157def test_base_url(driver, pages):158ctx = APIRequestContext(driver, base_url=pages.url(""))159response = ctx.get("simpleTest.html")160assert response.status == 200161assert "html" in response.text().lower()162ctx.dispose()163164165def test_isolated_context_no_browser_sync(driver, pages):166driver.delete_all_cookies()167isolated = driver.request.new_context()168isolated.get(pages.url("set_cookie?name=isolated_cookie&value=secret"))169assert driver.get_cookie("isolated_cookie") is None170isolated.dispose()171172173def test_storage_state_export(driver):174driver.add_cookie({"name": "export_cookie", "value": "export_val"})175state = driver.request.get_storage_state()176assert "cookies" in state177names = [c["name"] for c in state["cookies"]]178assert "export_cookie" in names179180181def test_storage_state_to_file(driver):182driver.add_cookie({"name": "file_cookie", "value": "file_val"})183with tempfile.NamedTemporaryFile(suffix=".json", delete=False, mode="w") as f:184tmp_path = f.name185try:186driver.request.get_storage_state(path=tmp_path)187data = json.loads(Path(tmp_path).read_text())188assert "cookies" in data189names = [c["name"] for c in data["cookies"]]190assert "file_cookie" in names191finally:192Path(tmp_path).unlink(missing_ok=True)193194195def test_new_context_with_storage_state(driver):196driver.add_cookie({"name": "state_cookie", "value": "state_val"})197with tempfile.NamedTemporaryFile(suffix=".json", delete=False, mode="w") as f:198tmp_path = f.name199try:200driver.request.get_storage_state(path=tmp_path)201isolated = driver.request.new_context(storage_state=tmp_path)202state = isolated.get_storage_state()203names = [c["name"] for c in state["cookies"]]204assert "state_cookie" in names205isolated.dispose()206finally:207Path(tmp_path).unlink(missing_ok=True)208209210def test_fetch_with_custom_method(driver, pages):211response = driver.request.fetch(pages.url("simpleTest.html"), method="GET")212assert response.status == 200213assert response.ok214215216def test_fail_on_status_code_raises_on_404(driver, pages):217with pytest.raises(APIRequestFailure) as exc_info:218driver.request.get(219pages.url("nonexistent_page_xyz.html"),220fail_on_status_code=True,221)222assert exc_info.value.response.status == 404223assert not exc_info.value.response.ok224assert "404" in str(exc_info.value)225assert "Not Found" in str(exc_info.value)226227228def test_fail_on_status_code_no_raise_on_200(driver, pages):229response = driver.request.get(230pages.url("simpleTest.html"),231fail_on_status_code=True,232)233assert response.status == 200234235236def test_fail_on_status_code_instance_default(driver, pages):237ctx = APIRequestContext(driver, fail_on_status_code=True)238with pytest.raises(APIRequestFailure):239ctx.get(pages.url("nonexistent_page_xyz.html"))240ctx.dispose()241242243def test_fail_on_status_code_per_request_overrides_default(driver, pages):244ctx = APIRequestContext(driver, fail_on_status_code=True)245response = ctx.get(246pages.url("nonexistent_page_xyz.html"),247fail_on_status_code=False,248)249assert response.status == 404250ctx.dispose()251252253