How to use prepare_image_attachment method in Lemoncheesecake

Best Python code snippet using lemoncheesecake

base_test.py

Source:base_test.py Github

copy

Full Screen

...172 with open(file, "w") as fh:173 fh.write(data)174 elif kind == "image":175 lcc.set_step("Prepare image attachment")176 with lcc.prepare_image_attachment(filename, file_description) \177 as file:178 with open(file, "w") as fh:179 fh.write(data)180 except FileNotFoundError:181 lcc.log_error("File or image does not exist!")182 @staticmethod183 def get_request(method_name, params=None):184 # Params must be list185 request = [1, method_name]186 if params is None:187 request.append([])188 return request189 request.extend([params])190 return request...

Full Screen

Full Screen

test_report_writer.py

Source:test_report_writer.py Github

copy

Full Screen

...396 report = run_func_in_test(do, tmpdir=tmpdir)397 assert_attachment(398 get_last_attachment(report), "foobar.txt", "some description", False, "some content", make_file_reader()399 )400def test_prepare_image_attachment(tmpdir):401 def do():402 with lcc.prepare_image_attachment("foobar.png", "some description") as filename:403 with open(filename, "wb") as fh:404 fh.write(SAMPLE_IMAGE_CONTENT)405 report = run_func_in_test(do, tmpdir=tmpdir)406 assert_attachment(407 get_last_attachment(report), "foobar.png", "some description", True, SAMPLE_IMAGE_CONTENT,408 make_file_reader(binary=True)409 )410def test_save_attachment_file(tmpdir):411 def do():412 filename = osp.join(tmpdir.strpath, "somefile.txt")413 with open(filename, "w") as fh:414 fh.write("some other content")415 lcc.save_attachment_file(filename, "some other file")416 report = run_func_in_test(do, tmpdir=tmpdir.mkdir("report"))...

Full Screen

Full Screen

test_selection.py

Source:test_selection.py Github

copy

Full Screen

1import pytest2from unittest.mock import MagicMock, patch3from callee import StartsWith, Any, Contains4from selenium.webdriver.common.by import By5from selenium.common.exceptions import WebDriverException, NoSuchElementException, TimeoutException6import lemoncheesecake.api as lcc7from lemoncheesecake.matching.matcher import MatchResult8from lemoncheesecake_selenium import Selector, Selection9from helpers import MyMatcher10FAKE_WEB_ELEMENT = object()11@pytest.fixture12def log_info_mock(mocker):13 return mocker.patch("lemoncheesecake_selenium.selection.lcc.log_info")14@pytest.fixture15def log_check_mock(mocker):16 return mocker.patch("lemoncheesecake.matching.operations.log_check")17@pytest.fixture18def prepare_image_attachment_mock(mocker):19 return mocker.patch("lemoncheesecake.api.prepare_image_attachment")20@pytest.fixture21def select_mock(mocker):22 return mocker.patch("lemoncheesecake_selenium.selection.Select")23@pytest.fixture24def preserve_selection_settings():25 orig_default_timeout = Selection.default_timeout26 orig_screenshot_on_exceptions = Selection.screenshot_on_exceptions27 orig_screenshot_on_failed_checks = Selection.screenshot_on_failed_checks28 yield29 Selection.default_timeout = orig_default_timeout30 Selection.screenshot_on_exceptions = orig_screenshot_on_exceptions31 Selection.screenshot_on_failed_checks = orig_screenshot_on_failed_checks32def test_element():33 mock = MagicMock()34 selector = Selector(mock)35 selection = selector.by_id("value")36 ret = selection.element37 assert isinstance(ret, MagicMock)38 mock.find_element.assert_called_with(By.ID, "value")39def test_element_not_found():40 mock = MagicMock()41 mock.find_element.side_effect = NoSuchElementException("value")42 selector = Selector(mock)43 selection = selector.by_id("value")44 with pytest.raises(NoSuchElementException):45 selection.element # noqa46def test_elements():47 mock = MagicMock()48 selector = Selector(mock)49 selection = selector.by_id("value")50 ret = selection.elements51 assert isinstance(ret, MagicMock)52 mock.find_elements.assert_called_with(By.ID, "value")53@pytest.mark.parametrize(54 "method_name,expected",55 (56 ("by_id", "element identified by id 'value'"),57 ("by_xpath", "element identified by XPATH 'value'"),58 ("by_link_text", "element identified by link text 'value'"),59 ("by_partial_link_text", "element identified by partial link text 'value'"),60 ("by_name", "element identified by name 'value'"),61 ("by_tag_name", "element identified by tag name 'value'"),62 ("by_class_name", "element identified by class name 'value'"),63 ("by_css_selector", "element identified by CSS selector 'value'")64 )65)66def test_str(method_name, expected):67 selector = Selector(None) # noqa68 selection = getattr(selector, method_name)("value")69 assert str(selection) == expected70def test_click(log_info_mock):71 mock = MagicMock()72 selector = Selector(mock)73 selection = selector.by_id("value")74 selection.click()75 assert mock.find_element.return_value.click.called76 log_info_mock.assert_called_with(StartsWith("Click on"))77def test_click_failure(log_info_mock):78 mock = MagicMock()79 mock.find_element.return_value.click.side_effect = WebDriverException()80 selector = Selector(mock)81 selection = selector.by_id("value")82 with pytest.raises(WebDriverException):83 selection.click()84def test_set_text(log_info_mock):85 mock = MagicMock()86 selector = Selector(mock)87 selection = selector.by_id("value")88 selection.set_text("content")89 mock.find_element.return_value.send_keys.assert_called_with("content")90 log_info_mock.assert_called_with(StartsWith("Set text"))91def test_set_text_failure(log_info_mock):92 mock = MagicMock()93 mock.find_element.return_value.send_keys.side_effect = WebDriverException()94 selector = Selector(mock)95 selection = selector.by_id("value")96 with pytest.raises(WebDriverException):97 selection.set_text("content")98def test_clear(log_info_mock):99 mock = MagicMock()100 selector = Selector(mock)101 selection = selector.by_id("value")102 selection.clear()103 mock.find_element.return_value.clear.assert_called()104 log_info_mock.assert_called_with(StartsWith("Clear"))105def test_clear_failure(log_info_mock):106 mock = MagicMock()107 mock.find_element.return_value.clear.side_effect = WebDriverException()108 selector = Selector(mock)109 selection = selector.by_id("value")110 with pytest.raises(WebDriverException):111 selection.clear()112def _test_select(log_info_mock, method_name, args, expected_log):113 mock = MagicMock()114 selector = Selector(mock)115 selection = selector.by_id("value")116 with patch("lemoncheesecake_selenium.selection.Select") as select_mock:117 getattr(selection, method_name)(*args)118 getattr(select_mock.return_value, method_name).assert_called_with(*args)119 log_info_mock.assert_called_with(expected_log)120@pytest.mark.parametrize(121 "method_name",122 ("select_by_value", "select_by_index", "select_by_visible_text")123)124def test_select(log_info_mock, method_name):125 _test_select(log_info_mock, method_name, ("arg",), StartsWith("Select"))126@pytest.mark.parametrize(127 "method_name,args",128 (("deselect_all", ()),129 ("deselect_by_value", ("opt",)), ("deselect_by_index", (1,)), ("deselect_by_visible_text", ("opt",)))130)131def test_deselect(log_info_mock, method_name, args):132 _test_select(log_info_mock, method_name, args, StartsWith("Deselect"))133def _test_select_failure(method_name, args):134 mock = MagicMock()135 selector = Selector(mock)136 selection = selector.by_id("value")137 with patch("lemoncheesecake_selenium.selection.Select") as select_mock:138 select_mock.side_effect = WebDriverException("error")139 with pytest.raises(WebDriverException):140 getattr(selection, method_name)(*args)141def test_select_failure_select_raises(log_info_mock):142 mock = MagicMock()143 selector = Selector(mock)144 selection = selector.by_id("value")145 with patch("lemoncheesecake_selenium.selection.Select") as select_mock:146 select_mock.side_effect = WebDriverException("error")147 with pytest.raises(WebDriverException):148 selection.select_by_index(1)149def test_select_failure_select_by_raises(log_info_mock):150 mock = MagicMock()151 selector = Selector(mock)152 selection = selector.by_id("value")153 with patch("lemoncheesecake_selenium.selection.Select") as select_mock:154 select_mock.return_value.select_by_index.side_effect = WebDriverException("error")155 with pytest.raises(WebDriverException):156 selection.select_by_index(1)157def test_check_element_success(log_check_mock):158 mock = MagicMock()159 mock.find_element.return_value = FAKE_WEB_ELEMENT160 selector = Selector(mock)161 selection = selector.by_id("value")162 matcher = MyMatcher()163 selection.check_element(matcher)164 log_check_mock.assert_called_with("Expect element identified by id 'value' to be here", True, None)165 assert matcher.actual is FAKE_WEB_ELEMENT166def test_check_element_failure_match(log_check_mock):167 mock = MagicMock()168 mock.find_element.return_value = FAKE_WEB_ELEMENT169 selector = Selector(mock)170 selection = selector.by_id("value")171 matcher = MyMatcher(result=MatchResult.failure())172 selection.check_element(matcher)173 log_check_mock.assert_called_with("Expect element identified by id 'value' to be here", False, None)174 assert matcher.actual is FAKE_WEB_ELEMENT175def test_check_element_failure_not_found(log_check_mock):176 mock = MagicMock()177 mock.find_element.side_effect = NoSuchElementException()178 selector = Selector(mock)179 selection = selector.by_id("value")180 matcher = MyMatcher(result=MatchResult.failure())181 selection.check_element(matcher)182 log_check_mock.assert_called_with(183 "Expect element identified by id 'value' to be here", False, StartsWith("Could not find")184 )185@pytest.mark.parametrize(186 "method_name", ("check_element", "require_element", "assert_element")187)188@pytest.mark.usefixtures("preserve_selection_settings")189def test_check_element_failure_match_with_screenshot(log_check_mock, prepare_image_attachment_mock, method_name):190 mock = MagicMock()191 mock.find_element.return_value = FAKE_WEB_ELEMENT192 selector = Selector(mock)193 selection = selector.by_id("value")194 matcher = MyMatcher(result=MatchResult.failure("Not found"))195 Selection.screenshot_on_failed_checks = True196 try:197 getattr(selection, method_name)(matcher)198 except lcc.AbortTest: # require_element and assert_element will raise AbortTest199 pass200 log_check_mock.assert_called_with("Expect element identified by id 'value' to be here", False, Any())201 prepare_image_attachment_mock.assert_called()202 assert matcher.actual is FAKE_WEB_ELEMENT203@pytest.mark.parametrize(204 "method_name,mock_raise_exception,abort_test_must_be_raised,check_outcome", (205 ("check_no_element", True, False, True),206 ("check_no_element", False, False, False),207 ("require_no_element", True, False, True),208 ("require_no_element", False, True, False),209 ("assert_no_element", True, False, None),210 ("assert_no_element", False, True, False)211 )212)213def test_check_no_element(log_check_mock,214 method_name, mock_raise_exception, abort_test_must_be_raised, check_outcome):215 mock = MagicMock()216 if mock_raise_exception:217 mock.find_element.side_effect = NoSuchElementException()218 else:219 mock.find_element.return_value = FAKE_WEB_ELEMENT220 selector = Selector(mock)221 selection = selector.by_id("value")222 if abort_test_must_be_raised:223 with pytest.raises(lcc.AbortTest):224 getattr(selection, method_name)()225 else:226 getattr(selection, method_name)()227 if check_outcome is not None:228 log_check_mock.assert_called_with(229 "Expect element identified by id 'value' to not be present in page", check_outcome, Any()230 )231# only perform basic tests on require_element & assert_element methods since they232# are simple calls to their lemoncheesecake counterparts233# the matcher wrapping system is already tested in depth the `test_check_element_*` tests234def test_require_element(log_check_mock):235 mock = MagicMock()236 mock.find_element.return_value = FAKE_WEB_ELEMENT237 selector = Selector(mock)238 selection = selector.by_id("value")239 matcher = MyMatcher()240 selection.require_element(matcher)241 log_check_mock.assert_called_with("Expect element identified by id 'value' to be here", True, None)242 assert matcher.actual is FAKE_WEB_ELEMENT243def test_assert_element(log_check_mock):244 mock = MagicMock()245 mock.find_element.return_value = FAKE_WEB_ELEMENT246 selector = Selector(mock)247 selection = selector.by_id("value")248 matcher = MyMatcher()249 selection.assert_element(matcher)250 log_check_mock.assert_not_called()251 assert matcher.actual is FAKE_WEB_ELEMENT252def test_with_must_be_waited_until():253 mock = MagicMock()254 mock.find_element.return_value = FAKE_WEB_ELEMENT255 selector = Selector(mock)256 selection = selector.by_id("value")257 selection.must_be_waited_until(lambda _: lambda _: True, timeout=0)258 assert selection.element is FAKE_WEB_ELEMENT259def test_with_must_be_waited_until_extra_args():260 mock = MagicMock()261 mock.find_element.return_value = FAKE_WEB_ELEMENT262 selector = Selector(mock)263 selection = selector.by_id("value")264 selection.must_be_waited_until(lambda arg1, arg2: lambda arg: True, timeout=0, extra_args=("value",))265 assert selection.element is FAKE_WEB_ELEMENT266def test_with_must_be_waited_until_failure():267 mock = MagicMock()268 mock.find_element.return_value = FAKE_WEB_ELEMENT269 selector = Selector(mock)270 selection = selector.by_id("value")271 selection.must_be_waited_until(lambda _: lambda _: False, timeout=0)272 with pytest.raises(TimeoutException):273 assert selection.element274@pytest.mark.usefixtures("preserve_selection_settings")275def test_with_must_be_waited_until_not_success():276 mock = MagicMock()277 mock.find_element.return_value = FAKE_WEB_ELEMENT278 selector = Selector(mock)279 selection = selector.by_id("value")280 Selection.default_timeout = 0281 selection.must_be_waited_until_not(lambda _: lambda _: False)282 assert selection.element is FAKE_WEB_ELEMENT283@pytest.mark.usefixtures("preserve_selection_settings")284def test_with_must_be_waited_until_not_extra_args():285 mock = MagicMock()286 mock.find_element.return_value = FAKE_WEB_ELEMENT287 selector = Selector(mock)288 selection = selector.by_id("value")289 Selection.default_timeout = 0290 selection.must_be_waited_until_not(lambda arg1, arg2: lambda arg: False, extra_args=("value",))291 assert selection.element is FAKE_WEB_ELEMENT292@pytest.mark.usefixtures("preserve_selection_settings")293def test_with_must_be_waited_until_not_failure():294 mock = MagicMock()295 mock.find_element.return_value = FAKE_WEB_ELEMENT296 selector = Selector(mock)297 selection = selector.by_id("value")298 Selection.default_timeout = 0299 selection.must_be_waited_until_not(lambda _: lambda _: True)300 with pytest.raises(TimeoutException):301 assert selection.element302@pytest.mark.parametrize(303 "action", (304 lambda s: s.click(),305 lambda s: s.clear(),306 lambda s: s.set_text("foo"),307 lambda s: s.select_by_value("foo"),308 lambda s: s.select_by_index(1),309 lambda s: s.select_by_visible_text("foo"),310 lambda s: s.deselect_all(),311 lambda s: s.deselect_by_index(1),312 lambda s: s.deselect_by_value("foo"),313 lambda s: s.deselect_by_visible_text("foo")314 )315)316@pytest.mark.usefixtures("preserve_selection_settings")317def test_screenshot_on_exception(log_info_mock, prepare_image_attachment_mock, action):318 driver_mock = MagicMock()319 driver_mock.find_element.side_effect = WebDriverException()320 selector = Selector(driver_mock)321 selection = selector.by_id("value")322 Selection.screenshot_on_exceptions = True323 with pytest.raises(WebDriverException):324 action(selection)325 prepare_image_attachment_mock.assert_called()326def test_save_screenshot(prepare_image_attachment_mock):327 mock = MagicMock()328 selector = Selector(mock)329 selection = selector.by_id("value")330 selection.save_screenshot()331 prepare_image_attachment_mock.assert_called_with(332 "screenshot.png", "Screenshot of element identified by id 'value'"333 )334 mock.find_element.return_value.screenshot.assert_called_with(Any())335def test_save_screenshot_with_description(prepare_image_attachment_mock):336 mock = MagicMock()337 selector = Selector(mock)338 selection = selector.by_id("value")339 selection.save_screenshot("some description")340 prepare_image_attachment_mock.assert_called_with(341 "screenshot.png", "some description"342 )343 mock.find_element.return_value.screenshot.assert_called_with(Any())344def test_save_screenshot_exception_raised_by_find_element(prepare_image_attachment_mock):345 mock = MagicMock()346 mock.find_element.side_effect = NoSuchElementException()347 selector = Selector(mock)348 selection = selector.by_id("value")349 with pytest.raises(NoSuchElementException):350 selection.save_screenshot()351def test_save_screenshot_exception_raised_by_screenshot(prepare_image_attachment_mock):352 mock = MagicMock()353 mock.find_element.return_value.screenshot.side_effect = WebDriverException()354 selector = Selector(mock)355 selection = selector.by_id("value")356 with pytest.raises(WebDriverException):...

Full Screen

Full Screen

session.py

Source:session.py Github

copy

Full Screen

...276 to write the attachment content.277 """278 return _prepare_attachment(filename, description)279@_interruptible280def prepare_image_attachment(filename, description=None):281 """282 Context manager. Prepare an image attachment using a pseudo filename and an optional description.283 The function returns the real filename on disk that will be used by the caller284 to write the attachment content.285 """286 return _prepare_attachment(filename, description, as_image=True)287def _save_attachment_file(filename, description=None, as_image=False):288 with _prepare_attachment(os.path.basename(filename), description, as_image=as_image) as report_attachment_path:289 shutil.copy(filename, report_attachment_path)290@_interruptible291def save_attachment_file(filename, description=None):292 """293 Save an attachment using an existing file (identified by filename) and an optional294 description. The given file will be copied....

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Lemoncheesecake automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful