معرفی کتاب «Python Testing with Selenium : Learn to Implement Different Testing Techniques Using the Selenium WebDriver» نوشتهٔ Lingo Mastery و Sujay Raghavendra، منتشرشده توسط نشر Apress : Imprint: Apress در سال 2021. این کتاب در فرمت pdf، زبان انگلیسی ارائه شده است.
«Python Testing with Selenium» نوشتهٔ سوجای راگهاوندرا، یکی از کاربردیترین و سریعترین منابع برای ورود به دنیای تست خودکار وبسایتها با استفاده از زبان برنامهنویسی پایتون و کتابخانهٔ محبوب سلنیوم وبدرایور است. این کتاب که توسط انتشارات معتبر Apress منتشر شده، با رویکردی گامبهگام و مبتنی بر مثال، به شما میآموزد که چگونه از صفر تا سطح پیشرفته، تستهای کارکردی (Functional Tests) را برای برنامههای تحت وب خود بنویسید و اجرا کنید.
دربارهٔ کتاب «Python Testing with Selenium» —
این کتاب با هدف پر کردن شکاف بین دانش مقدماتی پایتون و توانایی نوشتن تستهای حرفهای برای وباپلیکیشنها تألیف شده است. «Python Testing with Selenium» در ابتدا با مرور تاریخچه و معماری سلنیوم وبدرایور آغاز میشود و سپس به سراغ مباحث عملی مانند نصب و پیکربندی این ابزار برای مرورگرهای مختلف میرود. نویسنده با زبانی ساده و شفاف، دستورات پایه برای شروع نوشتن اسکریپتهای تست را توضیح میدهد و به تدریج خواننده را با مفاهیم پیچیدهتری مانند شبیهسازی تعاملات کاربر (کلیک، تایپ، کشیدن و رها کردن) و کار با عناصر مختلف صفحهٔ وب آشنا میسازد. یکی از نقاط قوت این کتاب، آموزش جامع روشهای شناسایی (Locate) عناصر وب است. سلنیوم هشت نوع شناساگر (Locator) مختلف برای یافتن المانهای صفحه در اختیار توسعهدهندگان قرار میدهد و این کتاب با ارائهٔ مثالهای عینی، به شما کمک میکند تا بهترین روش را برای نیاز خود انتخاب کنید. همچنین مباحث پیشرفتهتری مانند مدیریت استثناها (Exception Handling)، استفاده از الگوی طراحی Page Object Model برای افزایش قابلیت نگهداری کد، و نوشتن تستهای سازمانیافته با فریمورک Unittest در فصلهای پایانی کتاب پوشش داده شده است. تمام اسکریپتهای پایتون ارائه شده در کتاب، آمادهٔ اجرا بر روی مثالهای واقعی هستند و به صورت کامل توضیح داده شدهاند تا درک مطلب برای خواننده تسهیل شود.
دربارهٔ نویسنده
نویسندهٔ کتاب، سوجای راگهاوندرا (Sujay Raghavendra)، یک متخصص برجسته در حوزهٔ علم داده، یادگیری ماشین و یادگیری عمیق است و در حال حاضر سمت مدیر اجرایی شرکت مشاورهٔ Raghavendra Training & Consultancy را در هند بر عهده دارد. او دارای سابقهٔ همکاری با شرکتهای مختلف در زمینهٔ تحلیل و ارزیابی پروژههای فناورانه و توسعهٔ محصولات پژوهشی است و در پروژههای گوناگونی از جمله تحلیل تصاویر پزشکی، تحلیل تصاویر ماهوارهای و اتوماسیون شبکه فعالیت داشته است. راگهاوندرا همچنین مقالات پژوهشی متعددی در مجلات بینالمللی منتشر کرده و عضو کمیتههای داوری در کنفرانسها و ژورنالهای مختلف بوده است. او اخیراً به استفاده از یادگیری ماشین برای خودکارسازی موارد تست علاقهمند شده و کتاب دیگری نیز با عنوان «Beginner's Guide to Streamlit with Python» از او منتشر شده است.
چرا باید «Python Testing with Selenium» را بخوانید؟
- یادگیری سریع و عملی سلنیوم وبدرایور با پایتون: این کتاب با رویکرد مبتنی بر نحو (Syntax-Based) و ارائهٔ کدهای آماده اجرا، یک مرجع سریع برای شروع کار با سلنیوم است.
- آشنایی کامل با انواع روشهای شناسایی عناصر وب: شما با هشت نوع شناساگر مختلف (از جمله XPath و CSS Selector) آشنا شده و نحوهٔ استفاده از آنها را در پروژههای واقعی فرا میگیرید.
- شبیهسازی تعاملات پیچیده کاربر: کتاب به شما نشان میدهد که چگونه اعمالی مانند کلیک، دابلکلیک، کشیدن و رها کردن و استفاده از صفحهکلید را با استفاده از Action Chains در سلنیوم پیادهسازی کنید.
- نوشتن تستهای حرفهای و قابل نگهداری: با یادگیری الگوی طراحی Page Object Model و فریمورک Unittest، کدهای تست خود را سازماندهی کرده و از تکرار و پیچیدگی آن میکاهید.
- مدیریت استثناها و خطاهای رایج در تست: کتاب به شما میآموزد که با خطاهای رایج مانند پیدا نشدن عنصر یا عدم نمایش آن چگونه برخورد کرده و تستهای مقاومتری بنویسید.
این کتاب برای چه کسانی مناسب است؟
مخاطب اصلی این کتاب، توسعهدهندگان و تستکنندگان نرمافزار (Testers) هستند که با زبان پایتون آشنایی مقدماتی دارند و میخواهند دانش خود را در زمینهٔ تست خودکار برنامههای وب گسترش دهند. این کتاب هم برای افرادی که تازهکار هستند و هم برای متخصصانی که بهدنبال یک مرجع کامل و عملی برای سلنیوم وبدرایور میگردند، بسیار مفید خواهد بود. همچنین دانشجویان رشتههای علوم رایانه و مهندسی نرمافزار که به حوزهٔ تضمین کیفیت (QA) و تست نرمافزار علاقهمندند، میتوانند از محتوای کاربردی این کتاب بهرهمند شوند.
سوالات متداول
آیا برای شروع این کتاب، به دانش قبلی در مورد تست نرمافزار نیاز دارم؟
خیر. کتاب با مفروضات اولیه شروع میشود و حتی تاریخچه و معماری سلنیوم را نیز توضیح میدهد. با این حال، آشنایی پایه با زبان برنامهنویسی پایتون برای درک بهتر مثالهای کد ضروری است.
آیا کتاب فقط به تست برنامههای تحت وب با یک مرورگر خاص میپردازد؟
خیر. کتاب به طور کامل به نصب و پیکربندی سلنیوم برای مرورگرهای مختلف از جمله گوگل کروم، موزیلا فایرفاکس، مایکروسافت اج و اینترنت اکسپلورر میپردازد و نحوهٔ نوشتن تستهای سازگار با همهٔ آنها را آموزش میدهد.
آیا مثالهای کتاب فقط تئوری هستند یا قابل اجرا در دنیای واقعی؟
تمامی اسکریپتهای پایتون ارائه شده در کتاب، به گونهای طراحی شدهاند که بر روی مثالهای واقعی قابل اجرا باشند و هر کدام با بیان یک مسئله (Problem Statement) و راهحل کامل، توضیح داده شدهاند تا درک مطلب برای خواننده به حداکثر برسد.
Implement different testing techniques using Selenium WebDriver with the Python programming language. This quick reference provides simple functional test cases with a syntax-based approach for Selenium WebDriver. You’ll begin by reviewing the basics of Selenium WebDriver and its architectural design history and then move on to the configuration and installation of Selenium library for different web browsers, including the basic commands needed to start test scripts in various browsers. You’ll review action commands of keyboard and mouse for testing user interactions in a web page and see how hyperlinks are tested. The book also examines various web elements using eight different locators provided by Selenium to help you choose the one best suited to your needs. All Python scripts are ready to test real examples, all of which are explained thoroughly with problem statements. You’ll use different Python design patterns to automate test scripts that can be incorporated with Selenium. In the end, __Python Testing with Selenium__ will provide you with the expertise to write your own test cases in future. **What You’ll Learn** * Install and configure Selenium WebDriver with Python for different web-browsers * Review basic commands of Selenium * Locate web elements * Work with UI based web elements * Assert web elements and handle exceptions * Write test scripts in Page Object Model * Write test cases with Unittest framework **Who This Book Is For** Python developers/testers who want to test their web applications Table of Contents 5 About the Author 14 About the Technical Reviewer 15 Acknowledgments 16 Introduction 17 Chapter 1: Introduction to Selenium 18 What Is Selenium? 18 Selenium Tools and Versions 18 Selenium RC (Remote Control) 20 Selenium IDE 20 Selenium Grid 20 Selenium WebDriver 21 Selenium 2.0 21 Selenium 3.0 22 Selenium WebDriver Architecture 22 Client Library 22 JSON Wire Protocol 23 Web Drivers 23 Web Browsers 23 Why Selenium? 23 Open Source 24 Platforms 24 Language Support 24 Browser 24 Reuse 24 Easy Implementation 25 Flexible 25 Hardware Resources 25 Simulation 25 Other Testing Tools 25 Integrating Python with Selenium 27 Summary 31 Chapter 2: Getting Started 32 Installing Python 32 Installing Selenium 33 Installing Drivers 33 Browser Commands 34 Opening a Web Browser 34 Mozilla Firefox 35 Google Chrome 35 Microsoft Edge 35 Internet Explorer 35 Closing a Browser 36 Quit Browser 36 Opening a Web Page 37 Open a Page Online 37 Open a Page Offline 37 Setting the Browser Size 38 Maximize 38 Fullscreen 39 Setting the Size 39 Setting the Browser Position 39 Setting the Size Using Coordinates 40 Getting the Browser Position 40 Getting the Window Size 41 Navigation Commands 41 Back 41 Forward 42 Refresh 42 Running a Python Test Case 43 Summary 43 Chapter 3: Mouse and Keyboard Actions 44 Action Chains 44 Mouse 45 Click 45 Click and Hold 45 Context Click 46 Double Click 46 Move to an Element 47 Move Offset 48 Drag and Drop 49 Drag and Drop By 51 Release 52 Keyboard Actions 52 Key Down 53 Key Up 53 Send Keys 53 Send Keys to Element 54 Pause 54 Reset 54 Summary 54 Chapter 4: Web Elements 55 Elements/Web Elements 55 Web Locators 56 Locating Elements 56 ID Locator 56 Name Locator 57 XPath Locator 59 Nodes 61 Parents 61 Children 61 Siblings 61 Atomic Values 61 XPath Methods 61 Child 62 Parent 62 Self-Axis 62 Ancestor 62 Ancestor-or-Self 62 Descendant 62 Descendant or Self 62 Following 62 Following Sibling 63 Preceding 63 Preceding Sibling 63 Attribute 63 Namespace 63 Node Example 63 XPath Locator Types 66 Absolute Path 66 Relative Path 67 XPath with Logical Operators 68 AND 68 OR 69 Types of XPath Functions 70 contains() 70 text() 71 starts-with() 72 CSS Selectors 72 ID 73 Class 73 Substring Match Cases 74 Inner Text 75 CSS Selector for Multiple Attributes 75 CSS Selector for Child Elements 76 Link Locator 77 Partial Link Locator 78 Tag Name 79 Class Name 80 Locating Multiple Web Elements 81 Locator Issues 82 Attribute Change 83 No Web Element 83 Web Element Not Visible 83 Test Case Mismatch 84 iframe Web Element 84 Summary 84 Chapter 5: Navigation 85 Hyperlinks 85 Testing Hyperlinks 86 Hyperlink by ID 87 Hyperlink by Text 87 Hyperlink by Partial Link 87 Hyperlink by XPath 87 Nth Hyperlink 87 Return All Hyperlinks 88 Check for a Valid Hyperlink 88 Check for Broken Images 89 Data Attributes Hyperlink 90 Summary 90 Chapter 6: Buttons 91 Default Button 91 Select 92 Select by ID 92 By Text 93 By Name 93 Submit/Form Button 94 Select by Visible Text 95 Image as a Button 95 Assert for Button 96 Check the Presence of a Button 96 Check If the Button Is Enabled or Not 96 Radio Buttons 97 Select Radio Buttons 98 Simple Select 98 Select Using Label 99 Unselect/Clear Selection 99 Assert for Radio Button 100 Assert If a Radio Button 100 Assert If Checked 100 Checkbox 101 Select/Check 102 Check Using Name 102 Check Using ID 102 Assert If Checkbox 103 Assert Checkbox If Checked or Not 103 Clear/Unselect/Deselect Checkbox 103 Select List 104 Getting All the Options 106 Select 106 Select Using Visible Text 106 Select by Value 107 Select Using Index 107 Unselect/Clear/Deselect 108 Deselect/Clear One Selected 108 Deselect by Visible Text 108 Deselect by Value 108 Deselect by Index 109 Multiple Select List 109 Verify 110 Get the First Option Selected 111 Get All Options Selected 111 Deselect/Clear All Selected 111 Summary 112 Chapter 7: Frames and Textboxes 113 Iframe 113 Switching to Iframe 115 Switching Using ID 115 Switching Using Name 115 Switching Using Index 115 Switching as an Element 117 Switching to a Main Iframe 118 Default Content 118 Parent Frame 118 Frames with Waits 118 Textboxes 119 Single-line Textbox 119 Multiline Textbox 120 Inserting Values 121 Getting Values from Textbox and Textarea 121 Summary 122 Chapter 8: Assertions 123 The Need for Assertion 123 Basic Asserts 124 assertTrue 124 assertFalse 124 assertIs 125 assertIsNot 125 assertIsNone 125 assertIsNotNone 126 assertIsInstance 126 assertNotIsInstance 126 Compare Assert 126 assertEqual 127 assertNotEqual 127 assertGreater 128 assertGreaterEqual 128 assertLess 128 assertLessEqual 128 Collection Assert 129 Lists 129 Tuples 129 Sets 130 Dictionary 130 assertIn 131 assertNotIn 131 Summary 132 Chapter 9: Exceptions 133 What Is an Exception? 133 Why Use Exceptions? 133 Exceptions in Selenium 134 ConnectionClosedException 134 ElementClickInterceptedException 134 ElementNotInteractableException 134 ElementNotSelectableException 134 ElementNotVisibleException 134 ErrorInResponseException 135 ErrorHandler.UnknownServerException 135 ImeActivationFailedException 135 ImeNotAvailableException 135 InsecureCertificateException 135 InvalidArgumentException 136 InvalidCookieDomainException 136 InvalidCoordinateException 136 InvalidElementStateException 136 InvalidSelectorException 136 InvalidSessionIdException 136 InvalidSwitchToTargetException 137 JavascriptException 137 JsonException 137 MoveTargetOutOfBoundsException 137 NoAlertPresentException 137 NoSuchAttributeException 137 NoSuchCookieException 138 NoSuchElementException 138 NoSuchFrameException 138 NoSuchWindowException 138 NoSuchContextException 139 ScreenshotException 139 StaleElementReferenceException 139 TimeoutException 139 UnableToSetCookieException 139 UnexpectedAlertPresentException 139 UnexpectedTagNameException 140 UnknownMethodException 140 WebDriverException 140 Exception Handling 140 Timeout Exception 141 Element Not Found 141 Stale Element 142 Summary 143 Chapter 10: Waits 144 Why the Need for Waits? 145 Types of Waits 145 Implicit 146 Explicit 147 Commonly Used ExpectedConditions in the Python Class 149 Differences Between Implicit and Explicit 154 Fluent 155 Summary 157 Chapter 11: Page Objects 158 Page Objects Overview 158 Page Object Model (POM) Overview 159 The Need for the Page Objects Model 159 Creating Page Objects 160 Test.py 160 Page.py 161 Elements.py 162 Locators.py 163 Advantages of Page Objects Model 164 Limitations of the Page Object Model 165 Summary 165 Chapter 12: Using Test Cases with a Screenshot 166 Test Outcomes 166 Pass/OK 166 Fail 167 Error 167 Test Case 1: Taking a Screenshot 167 save_screenshot (‘name_of_file’) 167 get_screenshot_as_file (‘name_of_file’) 168 get_screenshot_as_png() 168 Testing Frameworks 169 The Unittest Framework 170 Test Case 2: Unittest 171 setUp() 171 tearDown() 172 setUpClass 173 tearDownClass 173 setUpModuleandtearDownModule 174 Test Execution Order 176 Testing Tool Comparisons 177 Summary 178 Index 179 Implement different testing techniques using Selenium WebDriver with the Python programming language. This quick reference provides simple functional test cases with a syntax-based approach for Selenium WebDriver. You'll begin by reviewing the basics of Selenium WebDriver and its architectural design history and then move on to the configuration and installation of Selenium library for different web browsers, including the basic commands needed to start test scripts in various browsers. You'll review action commands of keyboard and mouse for testing user interactions in a web page and see how hyperlinks are tested. The book also examines various web elements using eight different locators provided by Selenium to help you choose the one best suited to your needs. All Python scripts are ready to test real examples, all of which are explained thoroughly with problem statements. You'll use different Python design patterns to automate test scripts that can be incorporated with Selenium. In the end, Python Testing with Selenium will provide you with the expertise to write your own test cases in future. You will: Install and configure Selenium WebDriver with Python for different web-browsers Review basic commands of Selenium Locate web elements Work with UI based web elements Assert web elements and handle exceptions Write test scripts in Page Object Model Write test cases with Unittest framework Front Matter ....Pages i-xxi Introduction to Selenium (Sujay Raghavendra)....Pages 1-14 Getting Started (Sujay Raghavendra)....Pages 15-26 Mouse and Keyboard Actions (Sujay Raghavendra)....Pages 27-37 Web Elements (Sujay Raghavendra)....Pages 39-68 Navigation (Sujay Raghavendra)....Pages 69-74 Buttons (Sujay Raghavendra)....Pages 75-96 Frames and Textboxes (Sujay Raghavendra)....Pages 97-106 Assertions (Sujay Raghavendra)....Pages 107-116 Exceptions (Sujay Raghavendra)....Pages 117-127 Waits (Sujay Raghavendra)....Pages 129-142 Page Objects (Sujay Raghavendra)....Pages 143-150 Using Test Cases with a Screenshot (Sujay Raghavendra)....Pages 151-163 Back Matter ....Pages 165-170